admin管理员组文章数量:1334887
Is there a (simple?) way to test if a taxonomy has at least one tag that is used on more than one page?
I know how to see if there are any terms in a given taxonomy on the current page. And I know how to see if a given term on the current page is tagged on more than just the current page. Here is my code that works as written:
if( ! empty( wp_get_object_terms($post->ID, 'topic') ) ) {
echo '<p>Explore other posts with these topics:</p><div class="topic-list"><p class="meta-tag-list">';
$terms = wp_get_object_terms($post->ID, '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>';
}
}
echo '</p></div> <!-- topic-list -->';
}
But my first if statement just check to see if there are terms for that taxonomy on that page. But what it really needs to do is check if there is at least one term that has at least two pages tagged.
Because if there is a term for the current page, but that page is the only page for the term, then the first if statement executes and my heading "Explore other posts with these topics:" displays, but the second if statement doesn't execute. So I end up with a heading with nothing under it.
I think if the average number of total pages tagged with all terms is greater than one (total number of all pages tagged by terms this page is tagged with divided by total number of tags on this page) then that would meet the criterion. Is there an easy way to test that, or is there just a better way to do this?
Is there a (simple?) way to test if a taxonomy has at least one tag that is used on more than one page?
I know how to see if there are any terms in a given taxonomy on the current page. And I know how to see if a given term on the current page is tagged on more than just the current page. Here is my code that works as written:
if( ! empty( wp_get_object_terms($post->ID, 'topic') ) ) {
echo '<p>Explore other posts with these topics:</p><div class="topic-list"><p class="meta-tag-list">';
$terms = wp_get_object_terms($post->ID, '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>';
}
}
echo '</p></div> <!-- topic-list -->';
}
But my first if statement just check to see if there are terms for that taxonomy on that page. But what it really needs to do is check if there is at least one term that has at least two pages tagged.
Because if there is a term for the current page, but that page is the only page for the term, then the first if statement executes and my heading "Explore other posts with these topics:" displays, but the second if statement doesn't execute. So I end up with a heading with nothing under it.
I think if the average number of total pages tagged with all terms is greater than one (total number of all pages tagged by terms this page is tagged with divided by total number of tags on this page) then that would meet the criterion. Is there an easy way to test that, or is there just a better way to do this?
Share Improve this question asked Jul 14, 2020 at 7:51 HopefullCoderHopefullCoder 456 bronze badges1 Answer
Reset to default 1You already have all the logic that you need. You just need to rearrange things so that nothing is output until you know that there are terms. So instead of echoing each link immediately, save them to a variable, and then only output the introductory paragraph and wrapping elements if that variable isn't empty:
$terms = wp_get_object_terms($post->ID, 'topic');
$links = []; // We will add links to this.
foreach ($terms as $term) {
if ( $term->count > 1 ) { // Only add links for terms with more than one post.
$links[] = '<a href="' . esc_url( get_term_link( $term->term_id ) ) . '" rel="tag">' . esc_html( $term->name ) . '</a>'; // Add the link.
}
}
if ( ! empty( $links ) ) {
echo '<p>Explore other posts with these topics:</p><div class="topic-list"><p class="meta-tag-list">';
echo implode( $links );
echo '</p></div> <!-- topic-list -->';
}
本文标签: Test if taxonomy on current page has at least one term with more than one page tagged with that term
版权声明:本文标题:Test if taxonomy on current page has at least one term with more than one page tagged with that term 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742263851a2443016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论