列出在某个类别中写过帖子的作者

时间:2013-01-13 作者:Genxer

如何在WordPress中按类别列出帖子的作者?

在我的博客上有三位作者,我想在一个类别中列出写这些文章的人。例如:

CAT NAME: INTERNET     AUTHOR: JOHN, DOE, ALEX
CAT NAME: TECH         AUTHOR: JOHN
CAT NAME: CODE         AUTHOR: ALEX

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

我找到了解决办法。

代码:

  <?php
    $cat_arr = get_categories(); // Get the list of Categories
    foreach ($cat_arr as $cat_obj) {
        $term_id = $cat_obj->term_id;

        // Print the Name
        ?>
        <br>
        CAT NAME: <?php echo $cat_obj->name ?>, AUTHOR: 
        <?php
        // Get all Posts of that Category
        $posts = get_posts(array(\'category\'=>$term_id));

        $authors_arr = array();
        foreach ($posts as $post_obj) {
            $author_id = $post_obj->post_author;

                // In depends on where you put this code, the include of the file is required
            if (!function_exists(\'get_userdata\')) {
                include \'<your WP folder>/wp-includes/pluggable.php\';
            }

            $user_obj = get_userdata($author_id);
                // Only Add the author is isn\'t already added, to avoid printed twice
            if (!in_array($user_obj->user_login, $authors_arr)) {
                $authors_arr[] = $user_obj->user_login; // Instead of user_login you can use any Database field of the "Users" table
            }
        }
        echo implode(\', \', $authors_arr) . \'<br>\';
    }
    ?>

SO网友:JCL1178

WordPress类别没有传统意义上的作者。你应该仔细阅读WordPress Taxonomies 还可以查看您的wp\\u terms、wp\\u term\\u分类法和wp\\u term\\u关系表,了解WordPress如何组织和使用这些数据。

结束