1

Within a single post I show all tags of the specific post by using this:

the_tags( $tags_opening, ' ', $tags_ending );

But sometimes I mark a post with a tag that should not appear on that single page (but this tag shall still be found on - for example - an overview page with all tags).

How can I exclude specific tags by ID from this list of tags within the post (and just on the single page)?

migrated from webmasters.stackexchange.com Nov 28 '18 at 21:03

This question came from our site for pro webmasters.

1

Not sure that this is the best decision, but you can use the filter to cut your special tag without modifying templates.

add_filter('the_tags', 'wpse_320497_the_tags');

function wpse_320497_the_tags($tags) {
    $exclude_tag = 'word';

    // You can add your own conditions here
    // For example, exclude specific tag only for posts or custom taxonomy
    if(!is_admin() && is_singular()) {
        $tags = preg_replace("~,\s+<a[^>]+>{$exclude_tag}</a>~is", "", $tags);
    }

    return $tags;
});

If you confused with regexp, you can rewrite it with explode and strpos functions.

Hope it helps.

0

You can use get_terms() to get the terms of the post_tag taxonomy according to your needs. It offers parameters to achieve this, especially exclude or exclude_tree. All available parameters are documented at WP_Term_Query::__construct().

Then you basically recreate what the_tags() does in a custom function. To elaborate a bit, the_tags() calls get_the_tag_list() which calls get_the_term_list(). If you take a look at the documentation, source code you can easily create the same markup.

Generally speaking it might be worth considering a different approach, for example using a custom taxonomy to collect those terms you want to show on overview pages but not on single pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy