I want to generate short code for my gallery dynamically. For a single gallery, it is easy to create a short code for my gallery using

add_shortcode( 'image-gallery', 'gallery' )

Now my question is how can i create multiple short code for multiple gallery? Like for gallery 1 short code will be image-gallery-1, for 2 code will be image-gallery-2. I've tried this way

$all_gallery = $wpdb->prefix . 'gallery';
$all=$wpdb->get_results("select * from $all_gallery");
if(count($all)>0){
   foreach ($all as $a){
      add_shortcode( 'image-gallery-'.$a->id, 'gallery' );
   }
}

Its working nicely but when i deactivate the plugin and then activate the plugin again, a fatal error occurred and plugin is unable to activate.

  • Where is this code? Please include the context. – s_ha_dum Apr 21 '13 at 19:04

I cannot comment, I would if I could, but I can't, please don't down-vote

Could you not just use 1 shortcode with a parameter, e.g. instead of create one for each gallery image-gallery-1,image-gallery-2 etc, you could just use the ID as a parameter: image-gallery 1, image-gallery 2 ...

Then in your handling function you can simply test the parameter to see which gallery it is, I think this method is also slightly better for performance too as you don't need to call add_shortcode as many times, and all you are doing is removing one hyphen :)

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.