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 -->';
}