让人恼火的是count
是一个wp_term_taxonomy
桌子
因此,执行此操作的方法是自定义查询:
function wpse340250_term_count( WP_Term $term, $post_type) {
$q_args = [
\'post_type\' => $post_type,
\'nopaging\' => true, // no limit, pagination
\'fields\' => \'ids\', // only return post id\'s instead of full WP_Post objects will speed up
\'tax_query\' => array(
array(
\'taxonomy\' => $term->taxonomy,
\'field\' => \'term_id\',
\'terms\' => $term->term_id,
),
),
];
$term_count = get_posts($q_args);
return count($term_count);
}
因此,将行更改为:
$va_category_HTML .=\'<span class="post-count">\'.wpse340250_term_count($term, \'CUSTOM_POST_TYPE\').\'</span>\';
只需设置正确的posttype。