在自定义帖子类型中将帖子计数隐藏在帖子视图(全部删除、已发布和已废弃)之后

时间:2014-06-10 作者:Traveler

我需要隐藏/删除后端编辑屏幕后面的数字。

所有(30)|已发布(22)|草稿(5)|待定(2)|垃圾(1)

由于我正在运行一个多作者的博客,每个作者只能访问自己的帖子,我不想发布所有作者的累积信息。

使用以下代码,视图完全未设置,但我不想删除整个功能:

function remove__views( $views ) {
unset($views[\'all\']);
unset($views[\'publish\']);
unset($views[\'trash\']);
return $views;
}
add_action( \'views_edit-post\',  \'remove_views\' );
add_action( \'views_edit-movie\', \'remove_views\' );
有人知道我如何在编辑屏幕后面隐藏/删除数字,或者最多只显示与每个作者相关的数字吗?

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

不幸的是,没有“漂亮”的方法可以做到这一点(即不使用字符串替换或重写大量功能)。所以,求助于preg_replace...

我们需要过滤链接,很高兴看到您已经找到了合适的过滤器!通过遍历视图并使用正则表达式,我们可以删除包含post计数的元素。为帖子实现此功能,您将需要以下内容:

add_filter( \'views_edit-post\', \'wpse149143_edit_posts_views\' );

function wpse149143_edit_posts_views( $views ) {
    foreach ( $views as $index => $view ) {
        $views[ $index ] = preg_replace( \'/ <span class="count">\\([0-9]+\\)<\\/span>/\', \'\', $view );
    }

    return $views;
}

SO网友:W van Dam

我们今天遇到了同样的问题。一个漂亮的解决方案也许并不明显,但也并非不可能。

下面是我们用来只显示与每个作者相关的计数的代码。有权编辑其他用户帖子的用户仍将看到完整计数。

add_filter(\'wp_count_posts\', \'wpse149143_wp_count_posts\', 10, 3);


/**
 * Modify returned post counts by status for the current post type.
 *  Only retrieve counts of own items for users without rights to \'edit_others_posts\'
 *
 * @since   26 June 2014
 * @version 26 June 2014
 * @author  W. van Dam
 *
 * @notes   Based on wp_count_posts (wp-includes/posts.php)
 *
 * @param object $counts An object containing the current post_type\'s post
 *                       counts by status.
 * @param string $type   Post type.
 * @param string $perm   The permission to determine if the posts are \'readable\'
 *                       by the current user.
 * 
 * @return object Number of posts for each status
 */
function wpse149143_wp_count_posts( $counts, $type, $perm ) {
    global $wpdb;

    // We only want to modify the counts shown in admin and depending on $perm being \'readable\' 
    if ( ! is_admin() || \'readable\' !== $perm ) {
        return $counts;
    }

    // Only modify the counts if the user is not allowed to edit the posts of others
    $post_type_object = get_post_type_object($type);
    if (current_user_can( $post_type_object->cap->edit_others_posts ) ) {
        return $counts;
    }

    $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND (post_author = %d) GROUP BY post_status";
    $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type, get_current_user_id() ), ARRAY_A );
    $counts = array_fill_keys( get_post_stati(), 0 );

    foreach ( $results as $row ) {
        $counts[ $row[\'post_status\'] ] = $row[\'num_posts\'];
    }

    return (object) $counts;
}

SO网友:Javier Villanueva

我认为最简单的方法就是用CSS隐藏它们,您可以尝试以下方法:

add_action( \'admin_head\', \'wpse145565_hide_post_count\' );

function wpse145565_hide_post_count() {
    $css  = \'<style>\';
    $css .= \'.subsubsub a .count { display: none; }\';
    $css .= \'</style>\';

    echo $css;
}
基本上,该函数会在管理部分的头部添加一个样式标记,用于设置display 文本计数为的属性none.

SO网友:Manvi M

在WordPress 5.7中,上述代码工作不正常。例如,在“我的”的情况下,它不起作用。所以我修改了上面的代码。

function custom_editor_counts($views){
    
    $iCurrentUserID = get_current_user_id();

    global $wpdb;

    foreach ($views as $index => $view){
        
        if (in_array($index, array(\'all\')))
            continue;
        
        // There is no mine for post_status. However, it can be derived using the summation of pending and publish count.
        if(strcmp(\'mine\',$index)== 0){
            
            $viewValue = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type=\'post\' AND post_author = $iCurrentUserID and (post_status=\'pending\' || post_status=\'publish\' || post_status=\'draft\')");
            
        }else{
            
            $viewValue = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type=\'post\' AND post_author = \'" . $iCurrentUserID . "\' AND post_status=\'$index\'");
            
        }
        
        // Replace the existing value from the update one.
        $views[$index] = preg_replace(\'/\\(.+\\)/U\', \'(\' . $viewValue . \')\', $views[$index]);
        
    }


    unset($views[\'all\']);   
    return $views;

}

# It is for non admin users. For example contributors, etc.
if (!current_user_can(\'manage_options\')){
    add_filter("views_edit-post", \'custom_editor_counts\', 10, 1);
}
如需任何帮助,任何人都可以联系我。

结束

相关推荐

Display Search Result Count

到目前为止,我一直在使用下面的代码来获取搜索结果的数量,并显示该计数。<?php /* Search Count */ $allsearch =& new WP_Query(\"s=$s&showposts=-1\"); $count = $allsearch->post_count; echo $count . \' \'; wp_reset_query(); ?> 但这似乎不是有效的代码。显示以下错误:不推荐:不推荐通过引用分配new的返回值请任何人建议我获