我需要获得每个学期每种类型的帖子数量,所以我创建了这个小函数:
function get_term_post_count_by_type($term,$taxonomy,$type){
$args = array(
\'fields\' =>\'ids\', //we don\'t really need all post data so just id wil do fine.
\'posts_per_page\' => -1, //-1 to get all post
\'post_type\' => $type,
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'slug\',
\'terms\' => $term
)
)
);
$ps = get_posts( $args );
if (count($ps) > 0){return count($ps);}else{return 0;}
}
一旦有了这个函数,就可以将代码更改为:
<?php
$ptypes = array(\'post\',\'blog\',\'pic\'); //array with all of your post types
$tags = get_tags( array(\'name__like\' => "a", \'order\' => \'ASC\') );
foreach ( (array) $tags as $tag ) { ?>
<li>
<a href="<?php echo get_tag_link( $tag->term_id ) ?>">
<span class="name"><?php echo $tag->name ?></span>
<span class="number">
<?php
$count = 1;
foreach($ptypes as $t){
echo get_term_post_count_by_type($tag,\'post_tag\',$t) . " " . $t;
if (count($ptypes) != $count){
echo " | ";
$count = $count + 1;
}
}
?>
</span>
</a>
</li>
<?php } ?>