统计并列出用户在所有类别中发布的所有帖子

时间:2013-10-03 12:26:06

标签: javascript php wordpress charts

我刚刚发现了这个令人敬畏的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 ); ?>

有没有办法让我可以获得用户在所有类别中发布了多少帖子的类别细分

感谢您的帮助

3 个答案:

答案 0 :(得分: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 } ?>

感谢。

答案 1 :(得分: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";
}

答案 2 :(得分: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>