admin管理员组文章数量:1395752
At the bottom of a post, I show all that tags that post has.
If the current post is the only post with tag X, then I don't want tag X to show in the list. Because if someone clicks on that, they'll be taken to an archive page where the post they were just looking at is the only post listed. Useless.
Otherwise I don't have a problem with tags that only have one post.
It's a custom post type with custom taxonomies (three different taxonomies: topic, name, simile)
Here is how I am displaying the topic taxonomy:
<p class="meta-tag-list">
<?php
echo get_the_term_list( $post->ID, 'topic',
'', ' ', '' );
?> </p>
Does a function already exist that would let me do this?
At the bottom of a post, I show all that tags that post has.
If the current post is the only post with tag X, then I don't want tag X to show in the list. Because if someone clicks on that, they'll be taken to an archive page where the post they were just looking at is the only post listed. Useless.
Otherwise I don't have a problem with tags that only have one post.
It's a custom post type with custom taxonomies (three different taxonomies: topic, name, simile)
Here is how I am displaying the topic taxonomy:
<p class="meta-tag-list">
<?php
echo get_the_term_list( $post->ID, 'topic',
'', ' ', '' );
?> </p>
Does a function already exist that would let me do this?
Share Improve this question asked Mar 31, 2020 at 4:05 HopefullCoderHopefullCoder 456 bronze badges 1 |2 Answers
Reset to default 1Because get_the_term_list() create the html link for you. So
- you need to use wp_get_object_terms to get a list of terms info first with count for handling.
- compare each tag count, if the count > 1 then create the link and output
I have just tried. It works
<p class="meta-tag-list">
<?php
$terms = wp_get_object_terms($post->ID, 'category'); // replace 'category' (default Post post type taxonomy name) with your taxonomy such as 'topic'
foreach ($terms as $key => $term) {
if( $term->count > 1 ) { // if the count is > 1, output, if not, then nothing will happen
$link = get_term_link( $term->term_id );
echo '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
}
?>
</p>
you can try using a filter function (added into functions.php of your (child) theme), specific to your taxonomy 'topic': (based on https://developer.wordpress/reference/functions/get_the_term_list/ and https://developer.wordpress/reference/functions/get_the_terms/ )
// define the get_the_terms callback
function filter_get_the_topic_terms( $terms, $postid, $taxonomy ) {
if( !is_admin() && $taxonomy == 'topic' ) : // make filter magic happen here...
$filtered_terms = array();
foreach( $terms as $term ) :
if( $term->count > 1 ) {
$filtered_terms[] = $term;
}
endforeach;
$terms = $filtered_terms;
endif;
return $terms;
}
// add the filter
add_filter( 'get_the_terms', 'filter_get_the_topic_terms', 10, 3 );
本文标签: Don39t show a tag on a post if it is the only post with that tag
版权声明:本文标题:Don't show a tag on a post if it is the only post with that tag 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744617808a2615838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
terms variable
before passing toget_the_term_list()
. The spirit and principal in behind are same. If you have a lot of templates, then using a filter is an advantage. – 西門 正 Code Guy - JingCodeGuy Commented Mar 31, 2020 at 8:13