如何将作者类别中的帖子列表插入作者档案

时间:2012-03-01 作者:GlennFriesen

我想要的:http://i.imgur.com/jH6Dj.png (很抱歉链接,我是这里的新用户,因此无法在线发布图像)

如何在“作者存档”页面上列出类别中作者的帖子,以及每个类别的计数?

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

这不是最有效的方法,但它确实有效,请将其放入主题的功能中。php

function author_post_breakdown($author_id){
    //get all post of this author
    $n = get_posts(array(
        \'author\' => $author_id,
        \'posts_per_page\' => -1,
        \'fields\' => \'ids\'
        )
    );
    $cats= array();
    //loop over all post of this author and get the categories for each post
    foreach((array)$n as $id){
        $cs = wp_get_object_terms($id, \'category\');
        //loop over each category of the post and add the counter
        foreach($cs as $c){
            if (isset($cats[$c->slug])){
                $cats[$c->slug][\'count\'] = $cats[$c->slug][\'count\'] + 1;
            }else{
                $cats[$c->slug][\'count\'] = 0;
                $cats[$c->slug][\'name\'] = $c->name;
            }
        }
    }
    foreach($cats as $sl => $arr)
        $retVal = \'<li><a href="\'.get_term_link($slug, \'category\').\'">\'.$arr[\'name\'].\'(\'.$arr[\'count\'].\')</a></li>\'; 

    return $retVal;
}
还有你的作者。phpadd此:

$curauth = (isset($_GET[\'author_name\'])) ? get_user_by(\'slug\', $_GET[\'author_name\']) : get_userdata($_GET[\'author\']);
echo \'<ul>\'.author_post_breakdown($curauth->ID).\'</ul>\';

结束