-
Bladhark
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
-
Bladhark
By accident I marked this as solved, but it’s not solved yet.
-
David
Hi there,
GP uses the core
get_the_tag_list
function to display the list of post tags.
Which has theterm_links-{$taxonomy}
filter hookAnd 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 );
-
Bladhark
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:
No idea if it matters for the code but just to be sure, I’m using pages and not posts.
-
David
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.
-
Bladhark
I did that, but now it removes all tags it seems.
-
David
OK, start again.
Remove the codeRe 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
-
Bladhark
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.
-
Bladhark
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.
-
David
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 );
-
Bladhark
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).
-
David
Yeah it makes sense to give them some unique identifier.
Glad to hear you got it working.
- You must be logged in to reply to this topic.