0

我刚刚发现了这个很棒的 wordpress 功能

<?php echo 'Number of posts published by user: ' . count_user_posts( ); ?>

我正忙着制作一个图表,在饼图上显示用户在每个类别中完成了多少帖子。(chars.js)

有什么方法可以让我几乎可以在其中获取用户发布的每个类别的值的循环。

我想在未来证明它,所以如果添加更多类别,我不必去写这样的东西

<?php echo 'Number of posts published by user: ' . count_user_posts( 5 ); ?>
<?php echo 'Number of posts published by user: ' . count_user_posts( 7 ); ?>
<?php echo 'Number of posts published by user: ' . count_user_posts( 8 ); ?>

有没有一种方法可以让我获得用户在所有类别中发布的帖子数量的类别细分

谢谢你的帮助

4

3 回答 3

1

试试这个代码:

只需在数组中设置您想要的用户类型:

<?php $args = array(
    'blog_id'      => $GLOBALS['blog_id'],
    'role'         => 'subscriber',//"Super Admin" or "Administrator" or "Editor" or "Author" or "Contributor"
    'meta_key'     => '',
    'meta_value'   => '',
    'meta_compare' => '',
    'meta_query'   => array(),
    'include'      => array(),
    'exclude'      => array(),
    'orderby'      => 'login',
    'order'        => 'ASC',
    'offset'       => '',
    'search'       => '',
    'number'       => '',
    'count_total'  => false,
    'fields'       => 'all',
    'who'          => ''
 ); 

php get_users( $args );

foreach ($blogusers as $user) { ?>
        <li>
            <?php $user_id = $user->ID ?>
            <?php echo 'Number of posts published by user: ' . count_user_posts( $user_id ); ?>                 
        </li>   
<?php } ?>

谢谢。

于 2013-10-03T13:03:50.490 回答
0

我认为您误解了count_user_posts功能。它的参数是针对用户 ID 而不是针对类别 ID。

无论如何,一旦你有了所需的用户 ID(如果我理解得很好,你想显示用户是帖子作者的每个类别的帖子计数)你可以做这样的事情:

$user_id = 124;

/* Get all categories */

$categories = get_terms("category");

/* Loop for each category to count the posts of the user */
foreach($categories as $category)
{
   $cat_name = $category->name;
   $cat_id = $category->term_id;
   $post_count = count(get_posts("cat=$cat_id&post_author=$user_id"));

   echo "The user $user_id has $post_count posts in the $cat_name category";
}
于 2013-10-03T13:53:09.470 回答
0

这是完整的代码,谢谢大家的帮助

<script type="text/javascript">
var pieData = [
                            <?php
                                $user_id = get_query_var('author');

                                $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');

                                //get all posts from author
                                $args = array(
                                    'post_type' => 'post',
                                    'author'=> $queried_object->ID
                                );

                                $the_query = new WP_Query( $args );

                                if ( $the_query->have_posts() ) :

                                    while ( $the_query->have_posts() ) : $the_query->the_post();

                                        //put categories in array
                                        $cat = get_the_category( get_the_ID() );
                                        $terms[] = $cat[0]->term_id;

                                    endwhile;

                                    wp_reset_query();
                                endif;

                                //count matching categories (array vals)
                                $countVal = array_count_values($terms);
                                foreach($countVal as $count){

                                    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];

                                    echo "  {
                                            value: ".$count.",
                                            color:'".$color."'
                                            },";
                                }

                            ?>
                            ]
                            var myPie = new Chart(document.getElementById("piec").getContext("2d")).Pie(pieData);
                        </script>
于 2013-10-04T10:47:23.797 回答