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

categories - category list with cutom post count

programmeradmin1浏览0评论

i want to show category list with custom post type count. but i have two different post types in every category. how can i do that . please help. this is my code:

<?php
    $category_object           = get_queried_object();
    $current_category_taxonomy  = $category_object->taxonomy;
    $current_category_term_id  = $category_object->term_id;
     $current_category_name  = $category_object->name;

    $args = array(
        'child_of'            => $current_category_term_id,
        'current_category'    => $current_category_term_id,
        'depth'               => 0,
        'echo'                => 1,
        'exclude'             => '',
        'exclude_tree'        => '',
        'feed'                => '',
        'feed_image'          => '',
        'feed_type'           => '',
        'hide_empty'          => 0,
        'hide_title_if_empty' => false,
        'hierarchical'        => true,
        'order'               => 'ASC',
        'orderby'             => 'name',
        'separator'           => '',
        'show_count'          => 1,
        'show_option_all'     => '',
        'show_option_none'    => __( 'No categories' ),
        'style'               => 'list',
        'taxonomy'            => 'category',
        'title_li'            => __( $current_category_name ),
        'use_desc_for_title'  => 0,
    );
    wp_list_categories($args);
 ?>

i want to show category list with custom post type count. but i have two different post types in every category. how can i do that . please help. this is my code:

<?php
    $category_object           = get_queried_object();
    $current_category_taxonomy  = $category_object->taxonomy;
    $current_category_term_id  = $category_object->term_id;
     $current_category_name  = $category_object->name;

    $args = array(
        'child_of'            => $current_category_term_id,
        'current_category'    => $current_category_term_id,
        'depth'               => 0,
        'echo'                => 1,
        'exclude'             => '',
        'exclude_tree'        => '',
        'feed'                => '',
        'feed_image'          => '',
        'feed_type'           => '',
        'hide_empty'          => 0,
        'hide_title_if_empty' => false,
        'hierarchical'        => true,
        'order'               => 'ASC',
        'orderby'             => 'name',
        'separator'           => '',
        'show_count'          => 1,
        'show_option_all'     => '',
        'show_option_none'    => __( 'No categories' ),
        'style'               => 'list',
        'taxonomy'            => 'category',
        'title_li'            => __( $current_category_name ),
        'use_desc_for_title'  => 0,
    );
    wp_list_categories($args);
 ?>
Share Improve this question asked Aug 14, 2020 at 8:19 Gidromasservis QSCGidromasservis QSC 331 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Out of the box this cannot be done, as the post counts on each term is singular, there is no breakdown by post type.

In order to do this, you will need to output the markup yourself, and query for the counts yourself.

This will be slow/heavy/expensive.

So first we start with the current term/taxonomy:

$category_object           = get_queried_object();
$current_category_taxonomy  = $category_object->taxonomy;
$current_category_term_id  = $category_object->term_id;
$current_category_name  = $category_object->name;

Then we grab all child terms that aren't empty:

$children = get_terms(
    [
        'taxonomy' => $current_category_taxonomy,
        'parent'   => $category_object->term_id,
    ]
);

And loop over each term:

foreach ( $children as $child ) {
    //
}

Now inside that loop we need to check how many posts are of your particular post type, lets say the changeme post type:

    $args = [
        'post_type'      => 'changeme',
        $term->taxonomy  => $child->term_id,
        'posts_per_page' => 1,
    ];
    $q    = new WP_Query( $args );
    if ( $q->have_posts() ) {
        echo '<li>' . esc_html( $term->name ) . ' ( '. intval( $q->found_posts ) . ' ) </li>';
    }

Notice we used the found_posts parameter. At this point, it's just a matter of wrapping the whole thing in a <ul> tag, and expanding the markup to match what you want. You have the term object inside the loop to print out URLs etc with.

Don't forget, this is expensive/slow/heavy, you will want to cache this, perhaps using a transient, or if you have an object cache or Elastic search present.

Or you could just register another taxonomy specific for this post type, and avoid all of this.

function my_taxonomy_posts_count_func($atts)
    {
        extract(shortcode_atts(array(
            'post_type' => 'post',
        ) , $atts));
        global $WP_Views;
        $term = $WP_Views->get_current_taxonomy_term();
        $args = array(
            'post_type' => $post_type,
            $term->taxonomy => $term->term_id,
            'order' => 'ASC',
            'posts_per_page' => - 1,
        );
        $posts = get_posts($args);
        if ($posts)
        {
            $res = count($posts);
        }
        return $res;
    
    }
    add_shortcode('my-taxonomy-posts-count', 'my_taxonomy_posts_count_func');
  1. add codes in your theme/functions.php

  2. put the shortcode in your content, like this:

    [my-taxonomy-posts-count post_type="my-custom-post-type"]

  3. replace the "my-custom-post-type" with your specific post type slug

  4. replace the shortcode [wpv-taxonomy-post-count] with [my-taxonomy-posts-count post_type="my-custom-post-type"]

Change code as per comment

发布评论

评论列表(0)

  1. 暂无评论