wordpress exclude tag from get_the_tag_list

2019-05-11 08:09发布

While I use custom template tag to output single post tags:

<?php
echo get_the_tag_list('<p class="text-muted"><i class="fa fa-tags"></i> ',', ','</p>');
?>

How to exclude defined tag name from the tag list?

标签: php wordpress
1条回答
Deceive 欺骗
2楼-- · 2019-05-11 08:42

Well there is no filter to remove terms in get_the_tag_list but internally it calls for get_the_terms so you can add filter there.

Consider this example:-

add_filter('get_the_terms', 'exclude_terms');
echo get_the_tag_list('<p class="text-muted"><i class="fa fa-tags">',', ','</i></p>');
remove_filter('get_the_terms', 'exclude_terms');

Add a filter on get_the_terms and remove it after echoing list. Because it may be called on the page multiple times.

And in callback function remove terms by IDs or slugs

function exclude_terms($terms) {
    $exclude_terms = array(9,11); //put term ids here to remove!
    if (!empty($terms) && is_array($terms)) {
        foreach ($terms as $key => $term) {
            if (in_array($term->term_id, $exclude_terms)) {
                unset($terms[$key]);
            }
        }
    }

    return $terms;
}
查看更多
登录 后发表回答