如何显示网络帖子计数?

时间:2012-10-03 作者:TheLoneCuber

是否可以显示网络中所有博客的帖子总数?get_blog_countget_user_count 为博客和用户这样做,但我找不到任何方法来进行帖子计数。

2 个回复
SO网友:Nikky Southerland

在遇到内存执行问题时,对brasofilo的示例进行了一些调整;其中一个可能与switch_to_blog memory leak 这已经影响了网站switch_to_blog 调用(加载此仪表板小部件时,我们通常会运行大约1000次。)即使内存限制为256MB+;仍在获取执行错误。修复程序正在添加wp_cache_flush();

可能很粗糙,但清理的内存足以让它执行。其他更改还包括一些逻辑,以避免显示只有1个帖子的站点(可能是默认的),并显示包含2个以上帖子的“活动”站点总数,摘要位于表的底部。

    add_action( \'wp_network_dashboard_setup\', \'wpse_66963_network_dashboard_setup\' );

function wpse_66963_network_dashboard_setup()
{
    wp_add_dashboard_widget(
        \'wpse_66963_posts_count_widget\',
        \'<div id="icon-edit" class="icon32"></div><h2>Network Posts Count</h2>\',
        \'wpse_66963_posts_count\' );
}

function wpse_66963_posts_count()
{
    global $wpdb;
    $blogs = $wpdb->get_results( $wpdb->prepare(
            "SELECT * FROM {$wpdb->blogs} WHERE  spam = \'0\' 
            AND deleted = \'0\' AND archived = \'0\' 
            ORDER BY registered DESC, 2", ARRAY_A ) );

    $original_blog_id = get_current_blog_id();

    if ( empty( $blogs ) )
    {
        echo \'<p>No blogs!</p>\';
        break;
    }
    ?>
    <table class="widefat">
        <thead>
            <tr>
                <th>Site</th>
                <th>Total posts</th>
                <th>Draft posts</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
            <th>Site</th>
            <th>Total posts</th>
            <th>Draft posts</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
    $args = array(
        \'numberposts\'     => -1,
        \'post_type\'       => \'post\',
        \'post_status\'     => \'publish\' );
    $total_network = $draft_network = 0;
    $total_sites = 0;

    foreach ($blogs as $blog)
    {
        wp_cache_flush();
        switch_to_blog( $blog->blog_id );
        $args[\'post_status\'] = \'publish\';
        if (count(get_posts($args))<2) { continue; }
        $total_posts = count( get_posts( $args ) );
        $total_network += $total_posts;
        $total_sites += 1;

        $args[\'post_status\'] = \'draft\';
        $draft_posts = count( get_posts( $args ) );
        $draft_network += $draft_posts;
        ?>
           <tr>
             <td><a href="<?php echo site_url(); ?>"><?php echo site_url(); ?></a></td>
             <td><?php echo $total_posts; ?></td>
             <td><?php echo $draft_posts; ?></td>
           </tr>
        <?php
    }
    ?>
           <tr>
             <td><b>Total count (<?php echo $total_sites;?> sites with content)</b></td>
             <td><?php echo $total_network; ?></td>
             <td><?php echo $draft_network; ?></td>
           </tr>
        </tbody>
    </table>
<?php echo memory_get_usage(); ?>
<br/>
<?php echo memory_get_peak_usage(); ?>
    <?php
    switch_to_blog( $original_blog_id );
}

SO网友:brasofilo

以网络仪表板小部件的形式:

network dashboard widget

那个<h2> 因为小部件的标题有点过头了,但只是测试some styling :)

add_action( \'wp_network_dashboard_setup\', \'wpse_66963_network_dashboard_setup\' );

function wpse_66963_network_dashboard_setup() 
{
    wp_add_dashboard_widget( 
        \'wpse_66963_posts_count_widget\', 
        \'<div id="icon-edit" class="icon32"></div><h2>Network Posts Count</h2>\', 
        \'wpse_66963_posts_count\' );
}

function wpse_66963_posts_count() 
{
    global $wpdb;
    $blogs = $wpdb->get_results( $wpdb->prepare( 
            "SELECT * FROM {$wpdb->blogs} WHERE  spam = \'0\' 
            AND deleted = \'0\' AND archived = \'0\' 
            ORDER BY registered DESC, 5", ARRAY_A ) );

    $original_blog_id = get_current_blog_id();

    if ( empty( $blogs ) ) 
    {
        echo \'<p>No blogs!</p>\';
        break;
    }
    ?>          
    <table class="widefat">
        <thead>
            <tr>
                <th>Site</th>
                <th>Total posts</th>       
                <th>Draft posts</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
            <th>Site</th>
            <th>Total posts</th>
            <th>Draft posts</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
    $args = array(
        \'numberposts\'     => -1,
        \'post_type\'       => \'post\',
        \'post_status\'     => \'publish\' );
    $total_network = $draft_network = 0;

    foreach ($blogs as $blog) 
    {
        switch_to_blog( $blog->blog_id );

        $args[\'post_status\'] = \'publish\';
        $total_posts = count( get_posts( $args ) );
        $total_network += $total_posts;

        $args[\'post_status\'] = \'draft\';
        $draft_posts = count( get_posts( $args ) );
        $draft_network += $draft_posts;
        ?>
           <tr>
             <td><?php echo $blog->domain; ?></td>
             <td><?php echo $total_posts; ?></td>
             <td><?php echo $draft_posts; ?></td>
           </tr>
        <?php
    }
    ?>
           <tr>
             <td><b>Total count</b></td>
             <td><?php echo $total_network; ?></td>
             <td><?php echo $draft_network; ?></td>
           </tr>
        </tbody>
    </table>
    <?php
    switch_to_blog( $original_blog_id );
}

结束

相关推荐

Load posts dynamically

我正在寻找一个解决方案/指导,如何在模板的左列中以分层方式列出帖子链接,当选择链接时,它会在右列中加载帖子,但整个页面不会刷新?谢谢DeonUpdate for Marfarma\'s help below感谢您的回复。在阅读了许多页面和帖子后,我能够让它正常工作。我现在需要注意的一点是,右侧列中的链接的href值为#。我有一个主页设置,用户可以选择一条培训路径,将他们带到上面讨论的页面。但我在主页上也有单独的链接,以防用户想跳到特定的主题。因此,现在我的问题是,我可以对它们进行单独编码,并让它们转到单个