我想在 wordpress 中从我的自定义 post 中隐藏添加新内容,但是如果 posts 为空,则启用添加新内容

I want to hide add new from my custom post in wordpress but if posts are empty then enable add new

这是我的代码:

function wpmudev_create_slider_text() {
$labels = array(
    'name' => 'sliderText',
    'singular_name' => 'Slider ',
    'add_new' => 'Add Text',
    'edit_item' => 'Edit Text',
    'view_item' => 'View Text',
    'search_items' => 'Search Products',
    'not_found' => 'No Products Found',
    'not_found_in_trash' => 'No Products found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Slider Text',
);

register_post_type('product', array(
    'labels' => $labels,
    'has_archive' => true,
    'public' => true,
    'supports' => array('title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes'),
    'taxonomies' => array('post_tag', 'category'),
    'exclude_from_search' => false,
    'capability_type' => 'post',
    'capabilities' => array(
        'create_posts' => false,
    ),
    'map_meta_cap' => true,
    'rewrite' => array('slug' => 'sliderText'),
        )
);


}

add_action('init', 'wpmudev_create_slider_text');

如果有一条活动记录我想隐藏添加新如果没有活动记录(空)然后启用添加新插入一条记录

首先,您应该使用 wp_count_posts( 'product' )->publish 检查 active/publish post 的数量,然后使用条件语句可以禁用添加新功能。请检查以下代码:

function disable_create_newpost() {
  global $wp_post_types;
  if(wp_count_posts( 'product' )->publish > 0){
     $wp_post_types['product']->cap->create_posts = 'do_not_allow';
  }
}
add_action('init','disable_create_newpost');

https://codex.wordpress.org/Function_Reference/wp_count_posts