将自定义发布类型计数添加到仪表板

时间:2010-12-14 作者:Sampson

仪表板方便地告诉我有多少帖子、页面和评论。我想告诉我有多少文章,视频和卡通片我也(三个自定义的帖子类型注册我的主题)。如何将这些内容包含到仪表板上的“立即”面板中?

alt text

4 个回复
最合适的回答,由SO网友:Annika Backstrom 整理而成

是的,该小部件中有几个操作,包括right_now_content_table_end. 示例:

function my_right_now() {
    $num_widgets = wp_count_posts( \'widget\' );

    $num = number_format_i18n( $num_widgets->publish );
    $text = _n( \'Widget\', \'Widgets\', $num_widgets->publish );
    if ( current_user_can( \'edit_pages\' ) ) { 
        $num = "<a href=\'edit.php?post_type=widget\'>$num</a>";
        $text = "<a href=\'edit.php?post_type=widget\'>$text</a>";
    }   

    echo \'<tr>\';
    echo \'<td class="first b b_pages">\' . $num . \'</td>\';
    echo \'<td class="t pages">\' . $text . \'</td>\';
    echo \'</tr>\';
}
add_action( \'right_now_content_table_end\', \'my_right_now\' );

SO网友:Sébastien Méric

基于相同的代码,您可以使用此代码显示每个自定义帖子类型和自定义分类计数:

// Add custom taxonomies and custom post types counts to dashboard
add_action( \'right_now_content_table_end\', \'my_add_counts_to_dashboard\' );
function my_add_counts_to_dashboard() {
    // Custom taxonomies counts
    $taxonomies = get_taxonomies( array( \'_builtin\' => false ), \'objects\' );
    foreach ( $taxonomies as $taxonomy ) {
        $num_terms  = wp_count_terms( $taxonomy->name );
        $num = number_format_i18n( $num_terms );
        $text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name, $num_terms );
        $associated_post_type = $taxonomy->object_type;
        if ( current_user_can( \'manage_categories\' ) ) {
            $num = \'<a href="edit-tags.php?taxonomy=\' . $taxonomy->name . \'&post_type=\' . $associated_post_type[0] . \'">\' . $num . \'</a>\';
            $text = \'<a href="edit-tags.php?taxonomy=\' . $taxonomy->name . \'&post_type=\' . $associated_post_type[0] . \'">\' . $text . \'</a>\';
        }
        echo \'<td class="first b b-\' . $taxonomy->name . \'s">\' . $num . \'</td>\';
        echo \'<td class="t \' . $taxonomy->name . \'s">\' . $text . \'</td>\';
        echo \'</tr><tr>\';
    }

    // Custom post types counts
    $post_types = get_post_types( array( \'_builtin\' => false ), \'objects\' );
    foreach ( $post_types as $post_type ) {
        $num_posts = wp_count_posts( $post_type->name );
        $num = number_format_i18n( $num_posts->publish );
        $text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num_posts->publish );
        if ( current_user_can( \'edit_posts\' ) ) {
            $num = \'<a href="edit.php?post_type=\' . $post_type->name . \'">\' . $num . \'</a>\';
            $text = \'<a href="edit.php?post_type=\' . $post_type->name . \'">\' . $text . \'</a>\';
        }
        echo \'<td class="first b b-\' . $post_type->name . \'s">\' . $num . \'</td>\';
        echo \'<td class="t \' . $post_type->name . \'s">\' . $text . \'</td>\';
        echo \'</tr>\';

        if ( $num_posts->pending > 0 ) {
            $num = number_format_i18n( $num_posts->pending );
            $text = _n( $post_type->labels->singular_name . \' pending\', $post_type->labels->name . \' pending\', $num_posts->pending );
            if ( current_user_can( \'edit_posts\' ) ) {
                $num = \'<a href="edit.php?post_status=pending&post_type=\' . $post_type->name . \'">\' . $num . \'</a>\';
                $text = \'<a href="edit.php?post_status=pending&post_type=\' . $post_type->name . \'">\' . $text . \'</a>\';
            }
            echo \'<td class="first b b-\' . $post_type->name . \'s">\' . $num . \'</td>\';
            echo \'<td class="t \' . $post_type->name . \'s">\' . $text . \'</td>\';
            echo \'</tr>\';
        }
    }
}

SO网友:Brooke.

对于任何有兴趣更改“讨论”部分以显示待处理的帖子数量而不是将其全部显示在“内容”中的人right_now_discussion_table_end 挂钩可以这样使用:

add_action( \'right_now_discussion_table_end\', \'my_add_counts_to_rightnow_discussion\' );
function my_add_counts_to_rightnow_discussion() {
    // Custom post types counts
    $post_types = get_post_types( array( \'_builtin\' => false, \'public\' => true , \'show_ui\' => true), \'objects\' );
    foreach ( $post_types as $post_type ) {
            if ( current_user_can( \'edit_posts\' ) ) {

        $num_posts = wp_count_posts( $post_type->name );
         $text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num_posts->publish );

           $num = number_format_i18n( $num_posts->pending );
            $post_types = get_post_types( array( \'_builtin\' => false, \'public\' => true , \'show_ui\' => true), \'objects\' );

            $num = \'<a href="edit.php?post_status=pending&post_type=\' . $post_type->name . \'">\' . $num . \'</a>\';
                $text = \'<a class="waiting" href="edit.php?post_status=pending&post_type=\' . $post_type->name . \'">\' . $text . \' Pending </a>\';

            echo \'<td class="first b b-\' . $post_type->name . \'s">\' . $num . \'</td>\';
            echo \'<td class="t \' . $post_type->name . \'s">\' . $text . \'</td>\';
            echo \'</tr>\';
        }
    }
}
我还删除了if(pending >0) 这样,帖子类型将与左侧的计数对齐。如果使用此代码,只需使用Sébastien或Adam代码进行计数,并删除挂起的部分。

还要注意的是,我添加了一个检查,以查看帖子类型是否是公共的,并设置为显示在UI中。这可以添加到另一方的代码中。

SO网友:Chirag Pipariya
add_action( \'dashboard_glance_items\', \'cor_right_now_content_table_end\' );

function cor_right_now_content_table_end() {

    $args = array(
        \'public\' => true,
        \'_builtin\' => false
    );
    $output = \'object\';
    $operator = \'and\';

    $post_types = get_post_types( $args, $output, $operator );
    foreach ( $post_types as $post_type ) {
        $num_posts = wp_count_posts( $post_type->name );
        $num = number_format_i18n( $num_posts->publish );
        $text = _n( $post_type->labels->singular_name, $post_type->labels->name, intval( $num_posts->publish ) );
        if ( current_user_can( \'edit_posts\' ) ) {
            $output = \'<a href="edit.php?post_type=\' . $post_type->name . \'">\' . $num . \'&nbsp;\' . $text . \'</a>\';
        }
        echo \'<li class="post-count \' . $post_type->name . \'-count">\' . $output . \'</li>\';
    }
    $taxonomies = get_taxonomies( $args, $output, $operator );
    foreach ( $taxonomies as $taxonomy ) {
        $num_terms = wp_count_terms( $taxonomy->name );
        $num = number_format_i18n( $num_terms );
        $text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name, intval( $num_terms ) );
        if ( current_user_can( \'manage_categories\' ) ) {
            $output = \'<a href="edit-tags.php?taxonomy=\' . $taxonomy->name . \'">\' . $num . \'&nbsp;\' . $text . \'</a>\';
        }
        echo \'<li class="taxonomy-count \' . $taxonomy->name . \'-count">\' . $output . \'</li>\';
    }
}

// Add Some CSS to "At a Glance" Widget

function custom_colors() {

    echo \'<style type="text/css">
        .slides-count a:before {content:"\\f233"!important}
        .gallery-count a:before {content:"\\f163"!important}
        </style>\';
}

add_action(\'admin_head\', \'custom_colors\');
结束

相关推荐