按作者列出类别〜WITH COUNTER〜(Wordpress) [英] List categories by author ~ WITH COUNTER ~ (Wordpress)
问题描述
这是我得到的代码。它提供了给定作者发表的类别的列表。但是,我非常希望在类别名称旁边有一个数字,告诉作者在不同类别中发表了多少帖子。有人知道把戏吗?谢谢!
This is the code that I've got. It gives a list of the categories a given author has published in. However, I would very much like to have a number next to the category name, telling how many posts the author has published in the different categories. Anyone knows a trick? Thanks!
<?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>
编辑:
此代码计算在该类别中发帖,并且效果很好。我想将其与上面的代码结合起来,但我不知道该怎么做...
This code counts the posts in the category, and works fine. I want to combine this with the code above, but I don't know how to do it...
<?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;
?>
推荐答案
我知道了...必须运行单独的SELECT函数:一个用于获取类别列表,然后在该循环内提供另外一个函数以计算该类别中有多少个条目。我本来希望将这两个循环合而为一,但这对我来说很有效。
I figured it out... had to run to separate SELECT functions: one to fetch the list of categories, and then one more functions within that loop to count how many entries there is within the category. I would have preferred to have these two loops as one, but this works out for me.
<?php
// This will get us a list of the categories that our Author has published in
$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 posts.post_status = 'publish' AND
posts.post_author = '$author' AND
tax.taxonomy = 'category'
ORDER BY terms.name ASC
");
// This loop picks up categories
foreach($categories as $category) :
$catid = $category->ID;
// Now, inside the loop, we need to count how many posts that the Author has published.
$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 = $catid
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND post_author = '$author'
";
$user_count = $wpdb->get_var($counter);
echo '<div class="archive_author">' . $category->name . '<br/><span class="subcounter">' . $user_count . ' posts</span></div>';
endforeach;
?>
这篇关于按作者列出类别〜WITH COUNTER〜(Wordpress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!