好的,我看过一些关于如何在注册自定义帖子类型后更改其属性的类似帖子(在我的例子中,我试图修改由插件创建的自定义帖子类型,而不修改插件文件)。
这似乎适用于某些属性:
function change_wp_object() {
$object = get_post_type_object(\'easy-rooms\');
$object->show_in_menu = true;
}
add_action(\'init\',\'change_wp_object\');
然而,我似乎无法
has_archive
改变-这似乎与永久链接有关。我在猜测一条新规则
/easy-rooms/
未添加。
我尝试添加flush_rewrite_rules()
(尽管在init
) 但即使这样也不行。Post type设置为public,如果我更改has_archive
在插件代码中,它确实起作用。
有什么想法吗?提前感谢!
SO网友:Kudratullah
如果您的WordPress安装是4.4.0
或者更高,您可以过滤传递给register_post_type()
最终传递给WP_Post_Type::set_props
/**
* Customize Your Post Type args after post type has been registered.
*
* @param array $args Array of arguments for registering a post type.
* @param string $post_type Post type key.
*
* @return array
*/
function wpse_106784_filter_post_type_args( $args, $post_type ) {
if ( \'your-custom-post-type\' === $post_type ) {
$args[\'show_in_menu\'] = true;
$args[\'has_archive\'] = true;
// any other args you need to cange
}
return $args;
}
add_filter( \'register_post_type_args\', \'wpse_106784_filter_post_type_args\', 10, 2 );
Edit:
在将上述代码片段添加到代码中后,不要忘记更新永久链接设置。如果它是一个插件,并且您的插件有安装方法(或激活挂钩),那么您可以调用
flush_rewrite_rules()
在那里发挥作用。
来源:
https://developer.wordpress.org/reference/functions/register_post_type/#sourcehttps://developer.wordpress.org/reference/classes/wp_post_type/set_props/#source