在 wordpress 中创建一个隐藏的自定义 post 类型 - 但不是完全
Create a hidden custom post type in wordpress - but not totally
我需要注册一个自定义 post 类型,如“hidden_cpt”,即:
- public 站点不可见
- 没有管理菜单
- 只能通过 query_posts 或 wp_query
访问
- acf(或类似的技术低级插件)可见
我尝试用 register_post_type args 做一些测试,但它要么太隐蔽要么不够...
你有什么想法吗?
提前致谢
编辑:我尝试了以下代码,将一些“true”更改为“false”,反之亦然......但我没有保留所有测试:-(
这个不是最好的...
function register_hidden_cpt() {
$labels = [
"name" => __("HCPT", "hidden_cpt"),
"singular_name" => __("HCPT", "hidden_cpt"),
"menu_name" => __("HCPT", "hidden_cpt"),
];
$args = [
"label" => __("HCPT", "hidden_cpt"),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => false,
"query_var" => true,
"supports" => ["title", "editor", "thumbnail"],
];
register_post_type("hidden_cpt", $args);
}
add_action('init', 'register_hidden_cpt');
function register_hidden_cpt() {
$labels = [
"name" => __("HCPT", "hidden_cpt"),
"singular_name" => __("HCPT", "hidden_cpt"),
"menu_name" => __("HCPT", "hidden_cpt"),
];
$args = [
"label" => __("HCPT", "hidden_cpt"),
"labels" => $labels,
"public" => true, // for acf
"publicly_queryable" => true,
"show_ui" => false,
"show_in_rest" => false,
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => false,
"show_in_nav_menus" => false,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => false,
"query_var" => true,
"supports" => ["title", "editor", "thumbnail"],
];
register_post_type("hidden_cpt", $args);
}
add_action('init', 'register_hidden_cpt');
参考https://developer.wordpress.org/reference/functions/register_post_type/
一些数组值是 'parents' 到其他值,如果 parents 是假的,你不需要包括 child (除非你想要 child 参数显然要覆盖默认值)。好好看看上面的参考页面,阅读关于它们做什么的描述,你可以根据你的要求清理代码。通过 wp_query 几乎可以得到任何东西,但我无法访问 acf,因此婴儿提供对该请求的任何见解。
希望这对您有所帮助。祝你好运!
更新
最好在 ACF 论坛上提问。您还可以在您编写的任何附加功能中做一个条件,或者让它对管理员以外的任何人隐藏?例如,为 acf 设置 public,然后将其隐藏在您编写的任何函数中?我在 stackexchange 上找到了这个例子 - https://wordpress.stackexchange.com/questions/28782/possible-to-hide-custom-post-type-ui-menu-from-specific-user-roles
来源:@Milo (StackExchange)
要对 non-admin 用户隐藏 post 类型的菜单项:
function wpse28782_remove_menu_items() {
if( !current_user_can( 'administrator' ) ):
remove_menu_page( 'edit.php?post_type=your_post_type' );
endif;
}
add_action( 'admin_menu', 'wpse28782_remove_menu_items' );
your_post_type
应该是您实际 post 类型的名称。
我需要注册一个自定义 post 类型,如“hidden_cpt”,即:
- public 站点不可见
- 没有管理菜单
- 只能通过 query_posts 或 wp_query 访问
- acf(或类似的技术低级插件)可见
我尝试用 register_post_type args 做一些测试,但它要么太隐蔽要么不够...
你有什么想法吗?
提前致谢
编辑:我尝试了以下代码,将一些“true”更改为“false”,反之亦然......但我没有保留所有测试:-( 这个不是最好的...
function register_hidden_cpt() {
$labels = [
"name" => __("HCPT", "hidden_cpt"),
"singular_name" => __("HCPT", "hidden_cpt"),
"menu_name" => __("HCPT", "hidden_cpt"),
];
$args = [
"label" => __("HCPT", "hidden_cpt"),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => false,
"query_var" => true,
"supports" => ["title", "editor", "thumbnail"],
];
register_post_type("hidden_cpt", $args);
}
add_action('init', 'register_hidden_cpt');
function register_hidden_cpt() {
$labels = [
"name" => __("HCPT", "hidden_cpt"),
"singular_name" => __("HCPT", "hidden_cpt"),
"menu_name" => __("HCPT", "hidden_cpt"),
];
$args = [
"label" => __("HCPT", "hidden_cpt"),
"labels" => $labels,
"public" => true, // for acf
"publicly_queryable" => true,
"show_ui" => false,
"show_in_rest" => false,
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => false,
"show_in_nav_menus" => false,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => false,
"query_var" => true,
"supports" => ["title", "editor", "thumbnail"],
];
register_post_type("hidden_cpt", $args);
}
add_action('init', 'register_hidden_cpt');
参考https://developer.wordpress.org/reference/functions/register_post_type/
一些数组值是 'parents' 到其他值,如果 parents 是假的,你不需要包括 child (除非你想要 child 参数显然要覆盖默认值)。好好看看上面的参考页面,阅读关于它们做什么的描述,你可以根据你的要求清理代码。通过 wp_query 几乎可以得到任何东西,但我无法访问 acf,因此婴儿提供对该请求的任何见解。
希望这对您有所帮助。祝你好运!
更新
最好在 ACF 论坛上提问。您还可以在您编写的任何附加功能中做一个条件,或者让它对管理员以外的任何人隐藏?例如,为 acf 设置 public,然后将其隐藏在您编写的任何函数中?我在 stackexchange 上找到了这个例子 - https://wordpress.stackexchange.com/questions/28782/possible-to-hide-custom-post-type-ui-menu-from-specific-user-roles
来源:@Milo (StackExchange)
要对 non-admin 用户隐藏 post 类型的菜单项:
function wpse28782_remove_menu_items() {
if( !current_user_can( 'administrator' ) ):
remove_menu_page( 'edit.php?post_type=your_post_type' );
endif;
}
add_action( 'admin_menu', 'wpse28782_remove_menu_items' );
your_post_type
应该是您实际 post 类型的名称。