• Hello,

    I have a url, https://mysite.com//tag/symphony-orchestra+2000-2001-season/. I have these two tags + others tagged on my post, but I only want the one or two tags being passed in the url to be displayed on the tags.php page. Right now it is only displaying the first tag before the ‘+’, how can I get it to display both tags. This is my code.

     <?php $current_tag = $_GET['tag']; ?>
    
     <?php $current_category = single_cat_title("", false); ?>
      <?php 
      echo $_GET['tag'];
      $categories = single_term_title("", false); 
     
    if ( ! empty( $categories ) ) {
        echo '<h1>'.$categories.'</h1>';
        echo '<p>'.category_description( $category_id ).'</p>';	
    } ?>

    I have tried using the_tags(), but that displays all tags, even ones that are not in the url.

    Any Idea?

    Thanks,

    Aaron

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m unclear if you want only posts with tags in the URL to be listed, or if the posts listed is OK, you only want the tags in the URL to be shown with each post, not any others assigned.

    Either way, you need to extract the tags passed in the URL. The way WP parses permalinks, you will not be able to get all of them in the query’s “tag” query var nor from $_GET. You can get the entire URL less the domain.com part with $_SERVER[‘REQUEST_URI’]. Various parts can be extracted with preg_match().

    Then if you want to alter the posts returned, modify the query through the “pre_get_posts” action. To alter the tags that are shown, alter the tags.php template to only output post tags that match the ones extracted.

    As you’ve observed, using the + syntax in a permalink doesn’t work very well. It works reliably if you pass the tags as an URL parameter.
    example.com/?tag=symphony-orchestra+2000-2001-season+any-other-tags+you-care-to-pass
    Done this way, the query var and $_GET will have the correct values.

Viewing 1 replies (of 1 total)
  • The topic ‘Display only tags in the url on tags.php’ is closed to new replies.