wordpress 从 get_the_tag_list 中排除标签 [英] wordpress exclude tag from get_the_tag_list
本文介绍了wordpress 从 get_the_tag_list 中排除标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
虽然我使用自定义模板标签输出单个帖子标签:
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?
推荐答案
好吧,get_the_tag_list
中没有过滤器可以删除术语,但在内部它需要 get_the_terms
,因此您可以在那里添加过滤器.
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.
考虑这个例子:-
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');
在 get_the_terms
上添加过滤器,并在回显列表后将其删除.因为它可能在页面上被多次调用.
Add a filter on get_the_terms
and remove it after echoing list. Because it may be called on the page multiple times.
并在回调函数中通过 ID 或 slug 删除术语
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;
}
这篇关于wordpress 从 get_the_tag_list 中排除标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文