Home > Mobile >  How to show Tags inside Shortcode
How to show Tags inside Shortcode

Time:07-16

I know how to display Wordpress Current Post Title. It's like this:

<?php echo do_shortcode('[auto_gallery search="' . get_the_title($post_id) . '"]'); ?>

I also know how to display Tag without the link. It's like this:

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>

My question is, how do I display the Current Tags inside my Shortcode?

Because the method below doesn't work.

<?php echo do_shortcode('[auto_gallery search="' . get_the_tags($post_id) . '"]'); ?>

Any idea? Please also teach me how to display Current Category inside my Shortcode.

Thanks for your help, I really appreciate it.

CodePudding user response:

You can use this way to display tags inside a Shortcode:

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
echo do_shortcode('[auto_gallery search="' . $tag->name . '"]');
  }
}
?>

I hope this helps.

CodePudding user response:

<?php echo do_shortcode('[auto_gallery search="' . implode(" ",get_the_tags($post_id)) . '"]'); ?>

get_the_tags() function return the array of tags and you need to pass the string in the shortcode so simply array to string conversion.

  • Related