这是我得到的代码。它给出了给定作者发表的类别列表。但是,我非常希望在类别名称旁边有一个数字,告诉作者在不同类别中发表了多少帖子。有谁知道诀窍?谢谢!
<?php
$author = get_query_var('author');
$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE 1=1 AND (
posts.post_status = 'publish' AND
posts.post_author = '$author' AND
tax.taxonomy = 'category' )
ORDER BY terms.name ASC
");
?>
<ul>
<?php foreach($categories as $category) : ?>
<li>
<a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>">
<?php echo $category->name.' '.$category->description; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
编辑:
此代码计算类别中的帖子,并且工作正常。我想把它和上面的代码结合起来,但我不知道该怎么做......
<?php
$counter = "SELECT COUNT(*)
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = 412
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND post_author = '1'
";
$user_count = $wpdb->get_var($counter);
echo $user_count;
?>