Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 统计并列出用户在所有类别中发布的所有帖子_Javascript_Php_Wordpress_Charts - Fatal编程技术网

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

Javascript 统计并列出用户在所有类别中发布的所有帖子,javascript,php,wordpress,charts,Javascript,Php,Wordpress,Charts,我刚刚发现了这个很棒的wordpress功能 <?php echo 'Number of posts published by user: ' . count_user_posts( ); ?> 我正忙着制作一个图表,在饼图上显示每个类别的用户完成了多少篇文章。(chars.js) 是否有任何方法可以进行循环,几乎可以从中获取用户发布的每个类别的值 我想未来证明它,所以如果更多的类别被添加,我不必去写这样的东西 <?php echo 'Number of posts pu

我刚刚发现了这个很棒的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 ); ?>

有没有一种方法可以让我得到一个用户在所有类别中发布了多少篇文章的分类明细

感谢您的帮助

请尝试以下代码:

只需在阵列中设置所需的用户类型:

<?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 } ?>


  • 谢谢。

    我想你误解了功能。它的参数用于用户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";
    }
    

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

    <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>
    
    
    变量数据=[