在使用自定义分类法中的“重写”选项时,我的单个自定义帖子类型得到了404分。如果我将重写去掉,则单个页面可以工作,但自定义分类法存档可以工作,而404可以工作。
这是我自定义帖子类型的代码;
/* Training Post Type */
function add_training_post_type() {
$labels = array(
\'name\' => _x(\'Training\', \'post type general name\'),
\'singular_name\' => _x(\'Training material\', \'post type singular name\'),
\'add_new\' => _x(\'Add Training\',\'Training\'),
\'add_new_item\' => __(\'Add new Training\'),
\'edit_item\' => __(\'Edit Training\'),
\'new_item\' => __(\'New Training\'),
\'all_items\' => __(\'All Training\'),
\'view_item\' => __(\'View Training\'),
\'search_items\' => __(\'Search Training\'),
\'not_found\' => __(\'No Training found\'),
\'not_found_in_trash\' => __(\'No Training in Trash\'),
\'parent_item_colon\' => \'\',
\'menu_name\' => __(\'Training\')
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\')
);
register_post_type(\'training-and-events\',$args);
flush_rewrite_rules();
}
add_action( \'init\', \'add_training_post_type\' );
这是我的自定义分类代码;
/* Training Tax */
add_action( \'init\', \'create_training_taxonomies\', 0 );
function create_training_taxonomies()
{
$labels = array(
\'name\' => _x( \'Training categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Training category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Training categories\' ),
\'popular_items\' => __( \'Popular Training categories\' ),
\'all_items\' => __( \'All Training categories\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit Training category\' ),
\'update_item\' => __( \'Update Training category\' ),
\'add_new_item\' => __( \'Add Training category\' ),
\'new_item_name\' => __( \'New Training category\' ),
\'menu_name\' => __( \'Training categories\' )
);
register_taxonomy(\'training-category\',array(\'training-and-events\'), array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'training-and-events\')
));
flush_rewrite_rules();
}
(flush\\u rewrite\\u rules();就在我开发的时候,我会在它工作后删除)
有人知道我什么时候做得不对吗?我想我是瞎了眼,因为我盯着代码看了这么久,感觉就像是脑子里的意大利面条):
谢谢你,哈里。
最合适的回答,由SO网友:Marttin Notta 整理而成
将至少1个优先级添加到您的添加操作:
add_action( \'init\', \'add_training_post_type\', 1 );
add_action( \'init\', \'create_training_taxonomies\', 1 );
目前您的问题是,您的帖子类型和分类法重写段塞是相同的,因此URL库是相同的,这会混淆WP。幸运的是,有一个简单的解决方案,只需将此代码添加到函数中即可。php文件
function taxonomy_slug_rewrite($wp_rewrite) {
$rules = array();
// get all custom taxonomies
$taxonomies = get_taxonomies(array(\'_builtin\' => false), \'objects\');
// get all custom post types
$post_types = get_post_types(array(\'public\' => true, \'_builtin\' => false), \'objects\');
foreach ($post_types as $post_type) {
foreach ($taxonomies as $taxonomy) {
// go through all post types which this taxonomy is assigned to
foreach ($taxonomy->object_type as $object_type) {
// check if taxonomy is registered for this custom type
if ($object_type == $post_type->rewrite[\'slug\']) {
// get category objects
$terms = get_categories(array(\'type\' => $object_type, \'taxonomy\' => $taxonomy->name, \'hide_empty\' => 0));
// make rules
foreach ($terms as $term) {
$rules[$object_type . \'/\' . $term->slug . \'/?$\'] = \'index.php?\' . $term->taxonomy . \'=\' . $term->slug;
}
}
}
}
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(\'generate_rewrite_rules\', \'taxonomy_slug_rewrite\');
为了安全起见,我会转到您的永久链接页面,只需按“保存”按钮即可。