Search code examples
phpwordpresstagswordpress-shortcode

How to show Tags inside Shortcode


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.


Solution

  • 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.