doushi3715 2015-11-18 08:03
浏览 60

在wordpress中修改复数形式的views_edit

I want to change default Draft text for my custom post type views, and I found a function that could do this:

add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);

if (!function_exists('custom_draft_name')) {
    function custom_draft_name( $views ) {
        if (isset($views['draft']) && $views['draft'] != '') {
            $views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
            print_r($draft_array);
        }

        return $views;
    }
}

The problem is that I get only singular change for this. So if I had one Draft I'll have one Unapproved. But when I have more than one I'll have two Drafts and automatically I'll have two Unapproveds. Which is terrible. It should be two unapproved. So I tried to use _n() for the plurals, by creating an array to check against with my str_replace:

add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);

if (!function_exists('custom_draft_name')) {
    function custom_draft_name( $views ) {
        if (isset($views['draft']) && $views['draft'] != '') {
            $draft_array = array(esc_html__('Draft', 'custom_post_type_name'), _n('Draft', 'Drafts' , 10, 'custom_post_type_name'));
            $views['draft'] = str_replace($draft_array, esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
            print_r($draft_array);
        }

        return $views;
    }
}

But I still get Unapproveds instead Unapproved.

Any help how to solve this?

** ANSWER **

And once again I found the solution immediately after I post on SO xD

Just do a wp_count_posts('custom_post_type') like

$custom_posts = wp_count_posts('custom_post_type_name');
if($custom_posts->draft == 1){
    $views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
} else{
    $views['draft'] = str_replace(esc_html__('Drafts', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置