我的自定义帖子类型之一有一个奇怪的问题,该问题没有显示在左侧边栏管理菜单中。
我声明了 5 种自定义帖子类型,但第五种类型没有显示在左侧菜单中。这是未显示的客户帖子类型。我对此进行了很多搜索,但没有成功。
非常感谢您的帮助!
/**
* Custom Posts Types
*/
add_action('init', 'create_team_post_type');
function create_team_post_type() {
register_post_type( 'phil_team',
array(
'labels' => array(
'name' => __('Équipe'),
'singular_name' => __('Individu'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter un individu'),
'view_item' => __('Voir individu'),
'edit_item' => __('Modifier individu'),
'search_items' => __('Rechercher un individu'),
'not_found' => __('Individu non trouvé'),
'not_found_in_trash' => __('Individu non trouvé dans la corbeille')
),
'public' => true,
'hierarchical' => false,
'menu_position' => 21,
'rewrite' => array('slug' => 'team'),
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
}
add_action('init', 'create_projects_post_type');
function create_projects_post_type() {
register_post_type( 'phil_projects',
array(
'labels' => array(
'name' => __('Projets'),
'singular_name' => __('Projet'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter un projet'),
'view_item' => __('Voir projet'),
'edit_item' => __('Modifier projet'),
'search_items' => __('Rechercher un projet'),
'not_found' => __('Projet non trouvé'),
'not_found_in_trash' => __('Projet non trouvé dans la corbeille')
),
'public' => true,
'menu_position' => 21,
'query_var' => 'project',
'rewrite' => array('slug' => 'who-we-help/our-work'),
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
$set = get_option('post_type_rules_flased_POST-TYPE-NAME-HERE');
if ($set !== true){
flush_rewrite_rules(false);
update_option('post_type_rules_flased_POST-TYPE-NAME-HERE',true);
}
}
add_action('init', 'create_slideshow_post_type');
function create_slideshow_post_type() {
register_post_type( 'phil_home_slideshow',
array(
'labels' => array(
'name' => __('Slideshow'),
'singular_name' => __('Image'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter une image'),
'view_item' => __('Voir image'),
'edit_item' => __('Modifier image'),
'search_items' => __('Rechercher une image'),
'not_found' => __('Image non trouvé'),
'not_found_in_trash' => __('Image non trouvé dans la corbeille')
),
'public' => true,
'hierarchical' => false,
'menu_position' => 21,
'rewrite' => array('slug' => 'slideshow'),
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
}
add_action('init', 'create_home_boxes_post_type');
function create_home_boxes_post_type() {
register_post_type( 'phil_home_boxes',
array(
'labels' => array(
'name' => __('Boîtes page d\'accueil'),
'singular_name' => __('Boîte'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter une boîte'),
'view_item' => __('Voir boîte'),
'edit_item' => __('Modifier boîte'),
'search_items' => __('Rechercher une boîte'),
'not_found' => __('Boîte non trouvé'),
'not_found_in_trash' => __('Boîte non trouvé dans la corbeille')
),
'public' => true,
'hierarchical' => false,
'menu_position' => 21,
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
}
add_action('init', 'create_clients_post_type');
function create_clients_post_type() {
register_post_type( 'phil_clients',
array(
'labels' => array(
'name' => __('Clients'),
'singular_name' => __('Client'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter un client'),
'view_item' => __('Voir client'),
'edit_item' => __('Modifier client'),
'search_items' => __('Rechercher une client'),
'not_found' => __('Client non trouvé'),
'not_found_in_trash' => __('Client non trouvé dans la corbeille')
),
'public' => true,
'show_ui' => true,
'hierarchical' => false,
'menu_position' => 21,
'supports' => array('title', 'editor', 'thumbnail')
)
);
}
这种情况似乎不太一样,但在左侧管理菜单中不显示的原因之一是帖子类型的名称长度超过 20 个字符 功能参考/注册帖子类型
就我而言,我能够以管理员身份在左侧栏中看到自定义帖子类型,但作者却看不到。 我必须将 capability_type 从 page 更改为 post,因为这是作者默认启用的功能。
'capability_type' => 'post'
此行位于帖子类型定义的 $args 数组中。
如果您使用用户角色编辑器插件,有时需要添加(“用户”->“用户角色编辑器”->“添加功能”)一些自定义功能,例如:“read”、“edit_post”、“delete_post”、“手动 read_post' 以便能够(拥有权利)查看您的自定义帖子
有时,这可能是由于
menu_position
与另一个菜单位置冲突,或者可能被其他插件隐藏所致。尝试更改该值。
'menu_position' => 21
在
'show_in_menu' => true,
之后添加
'public' => true,
我也遇到了同样的问题,但没有任何帮助。但我找到了一个解决方案(暂时),我转到
Appearance > Customize
,就在那里,Custom post type
就在那里。我不知道是什么导致了这个错误,因为在本地主机中一切都很好,但在现场,它不起作用。
您可能刚刚将所需的属性设置为 false。这些属性必须设置为
true
才能使自定义帖子类型显示在左侧的 WordPress 管理菜单中:
'show_ui' => true,
'show_in_menu' => true,
如果这种情况仅发生在一个特定用户帐户上,但它适用于所有其他用户帐户,那么您需要让发生这种情况的用户执行以下操作:
在菜单页面中,转到右上角的“屏幕选项”,然后选中和取消选中您的自定义帖子类型。这会在内部刷新一些内容,并解决问题。如果单击“屏幕选项”后,您没有看到列出的自定义帖子类型,那么您需要使用此处发布的其他解决方案之一。
在您的
function.php
上添加代码。
参见下面定义的图像:
add_action('init', 'create_team_post_type');
function create_team_post_type() {
register_post_type( 'phil_team',
array(
'labels' => array(
'name' => __('Équipe'),
'singular_name' => __('Individu'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter un individu'),
'view_item' => __('Voir individu'),
'edit_item' => __('Modifier individu'),
'search_items' => __('Rechercher un individu'),
'not_found' => __('Individu non trouvé'),
'not_found_in_trash' => __('Individu non trouvé dans la corbeille')
),
'public' => true,
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 21,
'rewrite' => array('slug' => 'team'),
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
}
add_action('init', 'create_projects_post_type');
function create_projects_post_type() {
register_post_type( 'phil_projects',
array(
'labels' => array(
'name' => __('Projets'),
'singular_name' => __('Projet'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter un projet'),
'view_item' => __('Voir projet'),
'edit_item' => __('Modifier projet'),
'search_items' => __('Rechercher un projet'),
'not_found' => __('Projet non trouvé'),
'not_found_in_trash' => __('Projet non trouvé dans la corbeille')
),
'public' => true,
'has_archive' => true,
'menu_position' => 21,
'query_var' => 'project',
'rewrite' => array('slug' => 'who-we-help/our-work'),
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
$set = get_option('post_type_rules_flased_POST-TYPE-NAME-HERE');
if ($set !== true){
flush_rewrite_rules(false);
update_option('post_type_rules_flased_POST-TYPE-NAME-HERE',true);
}
}
add_action('init', 'create_slideshow_post_type');
function create_slideshow_post_type() {
register_post_type( 'phil_home_slideshow',
array(
'labels' => array(
'name' => __('Slideshow'),
'singular_name' => __('Image'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter une image'),
'view_item' => __('Voir image'),
'edit_item' => __('Modifier image'),
'search_items' => __('Rechercher une image'),
'not_found' => __('Image non trouvé'),
'not_found_in_trash' => __('Image non trouvé dans la corbeille')
),
'public' => true,
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 21,
'rewrite' => array('slug' => 'slideshow'),
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
}
add_action('init', 'create_home_boxes_post_type');
function create_home_boxes_post_type() {
register_post_type( 'phil_home_boxes',
array(
'labels' => array(
'name' => __('Boîtes page d\'accueil'),
'singular_name' => __('Boîte'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter une boîte'),
'view_item' => __('Voir boîte'),
'edit_item' => __('Modifier boîte'),
'search_items' => __('Rechercher une boîte'),
'not_found' => __('Boîte non trouvé'),
'not_found_in_trash' => __('Boîte non trouvé dans la corbeille')
),
'public' => true,
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 21,
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
}
add_action('init', 'create_clients_post_type');
function create_clients_post_type() {
register_post_type( 'phil_clients',
array(
'labels' => array(
'name' => __('Clients'),
'singular_name' => __('Client'),
'add_new' => __('Ajouter'),
'add_new_item' => __('Ajouter un client'),
'view_item' => __('Voir client'),
'edit_item' => __('Modifier client'),
'search_items' => __('Rechercher une client'),
'not_found' => __('Client non trouvé'),
'not_found_in_trash' => __('Client non trouvé dans la corbeille')
),
'public' => true,
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 21,
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
}
您的自定义帖子类型名称仅接受 20 个字符, 参见下面的示例,现在它显示在左侧的管理面板中 register_post_type( 'artedu_post_type', $args );
当时看不到 register_post_type( 'arteducation_post_type', $args );
将
show_in_menu
选项设置为 true
'show_in_menu' => true
在我的例子中,我使用在线自定义帖子类型生成器,由于某种原因,生成的代码将 show_in_menu 选项设置为 false
就我而言,问题是由键中的空格引起的: “发布类型密钥。不得超过 20 个字符,并且只能包含小写字母数字字符、破折号和下划线。”
参考:https://developer.wordpress.org/reference/functions/register_post_type/
使用 WP-CLI 这项工作很简单。首先创建所需的自定义帖子类型:
wp scaffold post-type team-member \
--label='Team Member' \
--dashicon='id-alt' \
--theme
最后,不要忘记添加到
functions.php
文件:
cat << EOF | tee -a $(wp eval 'echo get_theme_file_path("functions.php") . "\n";')
/**
* Custom Post Types.
*/
require get_template_directory() . '/post-types/team-member.php';
EOF