我注册了“rooms”的自定义帖子类型,以及自定义分类法。代码如下:
<?php
/*******************************************/
/* Rooms CPT
/*******************************************/
add_action(\'init\', \'room_register\');
function room_register() {
$labels = array(
\'name\' => _x(\'Rooms\', \'post type general name\'),
\'singular_name\' => _x(\'Room\', \'post type singular name\'),
\'add_new\' => _x(\'Add Room\', \'add button\'),
\'add_new_item\' => __(\'Add New Room\'),
\'edit_item\' => __(\'Edit Room\'),
\'new_item\' => __(\'New Room\'),
\'view_item\' => __(\'View Room\'),
\'search_items\' => __(\'Search Rooms\'),
\'not_found\' => __(\'Nothing found\'),
\'not_found_in_trash\' => __(\'Nothing found in Trash\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'has_archive\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'menu_icon\' => \'dashicons-screenoptions\',
\'supports\' => array(\'title\',\'editor\'),
\'rewrite\' => array( \'slug\' => \'rooms/%room_category%\', \'with_front\' => false )
//\'rewrite\' => array(\'slug\' => \'products\')
);
register_post_type( \'rooms\' , $args );
}
/*******************************************/
/* Rooms Taxonomy
/*******************************************/
add_action( \'init\', \'rooms_taxonomy\', 0 );
function rooms_taxonomy(){
$labels = array(
\'name\' => _x( \'Room Category\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Room Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Room Categories\' ),
\'all_items\' => __( \'All Room Categories\' ),
\'parent_item\' => __( \'Parent Room Category\' ),
\'parent_item_colon\' => __( \'Parent Room Category:\' ),
\'edit_item\' => __( \'Edit Room Category\' ),
\'update_item\' => __( \'Update Room Category\' ),
\'add_new_item\' => __( \'Add New Room Category\' ),
\'new_item_name\' => __( \'New Room Category\' ),
\'menu_name\' => __( \'Room Categories\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'rooms\', \'with_front\' => false ),
\'_builtin\' => false
);
register_taxonomy( \'room_category\', \'rooms\', $args );
}
但是,当我尝试访问网站上的url时:
http://www.upperhouse.org/rooms
我收到一个404错误。这不应该是分类法的登录页吗?这里很混乱。谢谢你的帮助!
SO网友:rudtek
您需要将永久链接刷新为@REactionFaye状态。更程序化的方法是使用刷新重写规则。
您可以从wordpress codex的本页中看到:https://codex.wordpress.org/Function_Reference/register_post_type:
如果在插件中注册post类型,请在激活和停用挂钩中调用flush\\u rewrite\\u rules()(请参阅下面的激活时刷新重写)。如果未使用flush\\u rewrite\\u rules(),则必须手动转到设置>永久链接并刷新永久链接结构,然后自定义帖子类型才能显示正确的结构。
如果代码位于插件中:
add_action( \'init\', \'my_cpt_init\' );
function my_cpt_init() {
register_post_type( ... );
}
function my_rewrite_flush() {
// First, we "add" the custom post type via the above written function.
// Note: "add" is written with quotes, as CPTs don\'t get added to the DB,
// They are only referenced in the post_type column with a post entry,
// when you add a post of this CPT.
my_cpt_init();
// ATTENTION: This is *only* done during plugin activation hook in this example!
// You should *NEVER EVER* do this on every page load!!
flush_rewrite_rules();
}
register_activation_hook( __FILE__, \'my_rewrite_flush\' );
如果在你的主题中:
add_action( \'init\', \'my_cpt_init\' );
function my_cpt_init() {
register_post_type( ... );
}
function my_rewrite_flush() {
my_cpt_init();
flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'my_rewrite_flush\' );
那一页上还有很多其他的好东西。
至于这个问题。您可能想检查房间类别的重写规则,看看是否可以通过以下方式加载它:(您的类别不应该是CPT名称。因此,我将“房间”改为“房间类型”。
\'rewrite\' => array( \'slug\' => \'room-types\', \'with_front\' => false ),
并将实际的CPT重写更改为:
\'rewrite\' => array( \'slug\' => \'rooms\', \'with_front\' => false )
最后,为了确保现在或将来没有冲突,特别是因为你已经在使用重写,你不应该在你的cpt中使用这样的通用术语。。。尝试添加前缀。ie:rt\\U房间。(这条提示也是抄本上的,但以前帮过我)
再次记住,在进行更改后,如果不使用刷新重写,则需要通过转到设置并保存永久链接页面手动刷新。