Use PHP Function instead of shortcode ?
-
Hello,
I was wondering if I can call the gallery by using a PHP function, instead of using the shorcode [mla_gallery] in a post.
Thanks for your answer, great plugin!
-
Thanks for the compliment and for your question.
The easy way to use
[mla_gallery]from PHP code is by calling the WordPressdo_shortcode()function. Here are two earlier topics with some examples:do_shortcode categories and taxonomies not working
Using
do_shortcode()is the best way to insulate your code from changes in future MLA versions. If you must, you can call the MLA shortcode processing functions directly, e.g.:echo MLAShortcodes::mla_gallery_shortcode( $attr, $content );A direct call gives you some flexibility in composing the shortcode parameters and avoiding some WordPress parsing issues but is somewhat more difficult to implement.
I am marking this topic resolved, but please update it if you have any problems or specific questions about calling
[mla_gallery]from your application’s PHP code. Thanks for your interest in the plugin.Hi
Thanks a lot!
I tried to use MLAShortcodes::mla_gallery_shortcode but just to be sure.My $attr array looks like :
$attr = array( 'post_mime_type' => 'all', 'tax_query' => array ('taxonomy' => 'attachment_tag', 'field' => 'id', 'terms' => array($wp_query->query_vars['photo_id']), 'include_children' => 0 ), 'mla_target' => 'blank', 'columns' => 2, 'posts_per_page' => 30, 'size' => 'large', 'link' => 'file', );Is it correct ?
Then I call echo MLAShortcodes::mla_gallery_shortcode( $attr, $content );
But I don’t see what $content is supposed to be 🙂For the moment, it makes my page crash and returns a blank document.
Thanks again for your help,
MarcThanks for reporting your progress and for adding the PHP code composing your
$attrarray. The syntax of the statement is fine and compiles correctly. There is one small error in your parameters; the'mla_target' => 'blank'parameter should be'mla_target' => '_blank'. The underscore is required for the HTMLtargetattribute.The
$contentparameter is optional; it contains the shortcode content when using the “enclosing shortcode” syntax. You can simply omit this parameter.The page crash might be because the
$wp_query->query_vars['photo_id']element is not defined. You can try something like this:if ( empty( $wp_query->query_vars ) ) { echo 'query_vars not defined'; elseif ( empty( $wp_query->query_vars['photo_id'] ) ) { echo 'photo_id not defined'; else { $attr = ... echo MLAShortcodes::mla_gallery_shortcode( $attr ); }That should help identify any problems.
-
This reply was modified 9 years, 2 months ago by
David Lingren.
-
This reply was modified 9 years, 2 months ago by
David Lingren.
Hi
Thanks a lot!
Actually my tax_query was incorrect, it is supposed to be a multidimensional array, not a single one.
All fixed now!Thanks again for your great support!
Good point – I looked at the syntax of the array statement but I didn’t investigate whether it was a valid WordPress
tax_query. I regret any inconvenience caused by my incomplete response.I am marking this topic resolved. Thanks for letting me know it’s working for you now.
-
This reply was modified 9 years, 2 months ago by
The topic ‘Use PHP Function instead of shortcode ?’ is closed to new replies.