Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    global $wp_query;
    $tags = $wp_query->get('tag');
    echo "Posts with tags: $tags";

    $tags will be something like “books+romance”, so you might want to string replace “+” with “, ” or similar before echoing out. This outputs the tag slugs. if you want the tag names, you’ll have to get each tag’s WP_Term object in turn, from which the tag name is available.

    Thread Starter Alkorr

    (@alkorr)

    Hi bcworkz! Thanks for your reply and the explaination, but I thought it would easier to achieve because I’m unfortunately not a coder (but I’m willing to learn) <:)

    Since WordPress supports multiple tag queries (in my case: https://www.mydomain.com/tag/books+romance/) I thought there was a ‘WordPress built-in’ way to show both tag names and not only the first one of them (in this case, Books).

    I tried to add the query you posted but it breaks my page… Could you please provide a more detailed or an easier way to do it? Thanks for your help! ??

    Moderator bcworkz

    (@bcworkz)

    My code works as expected on my site. My theme uses archive.php for such queries, YMMV. Be sure where you place the code is within a <?php ?> block, but be sure such blocks are not nested within another. If it’s among other PHP code, be sure the code is inserted on new lines after a terminal ; of other code.

    What I did with templates is copy archive.php from my parent theme (twentytwentyone) to my child theme, renaming it tag.php so it is only used for tag requests. I replaced this line with my code:
    the_archive_title( '<h1 class="page-title">', '</h1>' );
    and placed those same h1 tags on either side of my code, but outside of the `<?php ?> block. So

    <h1 class="page-title">
    <?php
    global $wp_query;
    $tags = $wp_query->get('tag');
    echo "Posts with tags: $tags";
    ?>
    </h1>
    Thread Starter Alkorr

    (@alkorr)

    Awesome, thank you very much bcworkz, it works perfectly! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to show name of tag+tag’ is closed to new replies.