最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to show tag count for individual product post in wordpress woocommerce

programmeradmin1浏览0评论

I am using WordPress Woocommerce as a review platform rather than a eCommerce site. When a review is made, a pre-set arrangement of tags are able to be used for each individual product post. Currently the tags on the product post show the total count site wide. For example, on page "product post A" tag "fast" shows "fast56" but tag "fast" is only used 2 times on "product post A". So it should only show "fast2". How can I make it so the number shows only the number a tag was used in a particular post?

I am using WordPress Woocommerce as a review platform rather than a eCommerce site. When a review is made, a pre-set arrangement of tags are able to be used for each individual product post. Currently the tags on the product post show the total count site wide. For example, on page "product post A" tag "fast" shows "fast56" but tag "fast" is only used 2 times on "product post A". So it should only show "fast2". How can I make it so the number shows only the number a tag was used in a particular post?

Share Improve this question edited Jul 9, 2019 at 0:42 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jul 8, 2019 at 23:41 user171531user171531 111 bronze badge 3
  • 1 Can you explain a bit more about your tags? It doesn't really make sense right now. As a tag (the post_tag taxonomy) can only be attached to a post once. Are you perhaps referencing to hash tags? – ngearing Commented Jul 9, 2019 at 4:43
  • I am using a plugin for grouping tags and I just spoke to the owner. He explained, "The count has the same link as the rest of the tag. The link always goes to a "tag archive" page where all posts are listed that have that tag. That is the normal behaviour of WordPress tag clouds. In order to change that, you need to change the code of the plugin. (Retrieve the permalink of the post with that particular ID and then replace the tag link with that permalink.)" – user171531 Commented Jul 10, 2019 at 5:59
  • prnt.sc/ocxryx If you look at the screenshot, it shows the total tag count for the entire site. I need the number associated with the tag to show only for that specific product on that page. – user171531 Commented Jul 10, 2019 at 6:02
Add a comment  | 

1 Answer 1

Reset to default 0

This isn't possible currently with what you've got. Tags or Taxonomies in WordPress can only bad attached to a post once. And are completely ignorant of the content in a post they are attached to.

However, it shouldn't be too difficult to write a function to count the times a tags name is used in the content of a post.

First, we need to run our counting function when a post is updated or created.

add_action( 'save_post', 'count_terms', 10, 2 );

Then our function to count the tags.

function count_terms( $post_id, $post ) {

    $taxonomy = 'post_tag'; // Set taxonomy to count.
    $terms    = get_the_terms( $post_id, $taxonomy );
    if ( ! $terms ) {
        return;
    }

    $term_counts = [];
    foreach ( $terms as $term ) {
        // Words to search the content for.
        $term_search = [];
        $term_search[] = $term->name;
        $term_search[] = $term->slug;
        $term_search = implode('|', $term_search);

        /* Search content excluding text in html tags. '(?!([^<]+)?>)' */
        preg_match_all( "/(?>\b)(?:$term_search)(?>\b)(?!([^<]+)?>)/mi", $post->post_content, $matches );

        // Count matches & push to array.
        if ( $matches ) {
            $term_counts[ $term->slug ] = count( $matches[0] );
        } else {
            $term_counts[ $term->slug ] = 0;
        }
    }

    // Save count.
    update_post_meta( $post_id, "{$taxonomy}_content_counts", $term_counts, get_post_meta( $post_id, "{$taxonomy}_content_counts", true ) );

}

And now we need some way to display the counts. You could add this function to your theme template.

function display_term_counts( $post_id, $taxonomy = 'post_tag', $echo = false ) {

    if ( ! $post_id ) {
        $post_id = get_the_id();
    }

    $counts = get_post_meta( $post_id, "{$taxonomy}_content_counts", true );
    if ( ! $counts ) {
        return false;
    }

    $output = [];

    foreach ( $counts as $term => $count ) {
        $term = get_term_by( 'slug', $term, $taxonomy );

        $output[] = sprintf(
            '<span class="term-count">%s - %s</span>',
            $term->name,
            $count
        );

    }

    // Could add separator here.
    $output = implode( '', $output );

    $output = "<div class='term-counts'>$output</div>";

    if ( $echo ) {
        echo $output;
    }

    return $output;
}

Or wrap it in a shortcode and use it in the content. [term_counts tax='post_tag']

add_shortcode( 'term_counts', function( $atts ) {
    $tax = $atts['tax'];
    if ( ! $tax ) {
        return false;
    }

    return display_term_counts( get_the_id(), $tax );
} );
发布评论

评论列表(0)

  1. 暂无评论