问题描述

仪表板方便地告诉我有多少帖子,页面和评论。我想告诉我有多少文章,视频和漫画 (三个自定义的帖子类型注册我的主题) 。我将如何将其包含在仪表板上的”Right Now” 面板中?

最佳解决方案

是的,该小工具中有几个操作,包括 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' );

次佳解决方案

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

// 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='%20.%20$taxonomy->name%20.%20'&post_type='%20.%20$associated_post_type[0]%20.%20'">' . $num . '</a>';
            $text = '<a href="edit-tags.php?taxonomy='%20.%20$taxonomy->name%20.%20'&post_type='%20.%20$associated_post_type[0]%20.%20'">' . $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='%20.%20$post_type->name%20.%20'">' . $num . '</a>';
            $text = '<a href="edit.php?post_type='%20.%20$post_type->name%20.%20'">' . $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='%20.%20$post_type->name%20.%20'">' . $num . '</a>';
                $text = '<a href="edit.php?post_status=pending&post_type='%20.%20$post_type->name%20.%20'">' . $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>';
        }
    }
}

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。