Php 我想在wordpress中隐藏自定义帖子中的添加新内容,但如果帖子为空,则启用添加新内容
这是我的密码:Php 我想在wordpress中隐藏自定义帖子中的添加新内容,但如果帖子为空,则启用添加新内容,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,这是我的密码: 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_ite
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\u count\u posts('product')->publish检查活动/发布帖子的数量,然后使用条件语句,您可以禁用添加新功能。请检查以下代码:
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');