Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Exclude certain tags from pages

  • I’m using a plugin and the following code to show tags on pages as I”m not using the internal WP structure for categories.

    add_filter( ‘generate_footer_meta_post_types’, function( $types ) {
    $types[] = ‘page’;

    return $types;
    } );

    add_action(‘generate_after_content’,function(){
    if ( is_singular(‘page’)) {
    generate_footer_meta();
    }
    });

    This works fine. However, I want to exclude some specific tags from showing there as they are only for internal use. I’ve seen some solutions on the forum

  • By accident I marked this as solved, but it’s not solved yet.

  • Hi there,

    GP uses the core get_the_tag_list function to display the list of post tags.
    Which has the term_links-{$taxonomy} filter hook

    And you can use that hook to exclude specific terms from your tag list.

    Try adding this PHP Snippet to do that:

    function db_exclude_tag_terms( $term_links ) {
        $result = array();
        $exclude_tags = array( 'a tag', 'another tag', 'and another tag' );
        foreach ( $term_links as $link ) {
            foreach ( $exclude_tags as $tag ) {
                if ( stripos( $link, $tag ) !== false ) continue 2;
            }
            $result[] = $link;
        }
        return $result;
    }
    add_filter( "term_links-post_tag", 'db_exclude_tag_terms', 99, 1 );
  • Hello David,

    Thanks for your reply.

    I added this in the Theme Functions file from the child theme but the specific tags are still showing:

    View post on imgur.com

    No idea if it matters for the code but just to be sure, I’m using pages and not posts.

  • In this line:

    $exclude_tags = array( 'a tag', 'another tag', 'and another tag' );

    you need to change the names in that array to the terms you want to exclude from the list.

  • I did that, but now it removes all tags it seems.

  • OK, start again.
    Remove the code

    Re add the code i provided here:

    https://generate.support/topic/exclude-certain-tags-from-pages/#post-7788

    AND THEN ONLY make the change i provided here:

    https://generate.support/topic/exclude-certain-tags-from-pages/#post-7807

  • Ok we are almost there.

    The code above works now, but it seems to remove all tags that include those words, and some tags share the same words.

    I tried to use the tag id’s instead, but that didn’t work.

  • I somewhat fixed it, I gave all the tags that I don’t want to show the same “internal” addition to their name and I excluded that via the above code.

    So now whenever I want a tag to not show on the frontend, I’ll add that line of text in the tag name.

  • Try this instead:

    function db_exclude_tag_terms( $term_links ) {
        $result = array();
        $exclude_tags = array( 'a tag', 'another tag', 'and another tag' );
        foreach ( $term_links as $link ) {
            foreach ( $exclude_tags as $tag ) {
    		if ( strip_tags( $link ) == $tag ) continue 2;
            }
            $result[] = $link;
        }
        return $result;
    }
    add_filter( "term_links-post_tag", 'db_exclude_tag_terms', 99, 1 );
  • I’ll do that when it’s not working out properly with the previous code.

    I’m happy how it works now, as right now I don’t have to update that code with new tags each time I add new ones (will be a looooot of tags that I’m going to add).

  • Yeah it makes sense to give them some unique identifier.

    Glad to hear you got it working.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.