我有自定义帖子类型和此自定义帖子类型的3个自定义分类。我为分类法重写了slug。但是所有分类归档都返回404错误,但最后一个分类归档返回200。如果我创建新分类法,最后一个分类法也返回404错误,而新分类法的代码是200。这是怎么解决的?
我的代码:
add_action('init', 'main_avto_post_type');
function main_avto_post_type(){
register_post_type('main_avto', array(
'labels' => array(
'name' => 'Schools', // Основное название типа записи
'singular_name' => 'School', // отдельное название записи типа Book
'add_new' => 'Add new',
'parent_item_colon' => '',
'menu_name' => 'Schools'
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '/%main-city%', 'with_front' => false ),
'has_archive' => 'avtoschools',
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
) );
}
add_action( 'init', 'create_main_avto_taxonomies' );
function create_main_avto_taxonomies(){
register_taxonomy('main-city', array('main_avto'), array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'City', 'taxonomy general name' ),
'singular_name' => _x( 'City', 'taxonomy singular name' ),
'search_items' => __( 'Search' ),
'all_items' => __( 'All city' ),
'menu_name' => __( 'City' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '/', 'with_front' => false ),
));
register_taxonomy('main_subway', array('main_avto'), array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Subway', 'taxonomy general name' ),
'singular_name' => _x( 'Subway', 'taxonomy singular name' ),
'search_items' => __( 'Search' ),
'all_items' => __( 'All Subway' ),
'menu_name' => __( 'Subway' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '/', 'with_front' => false ),
));
register_taxonomy('main_district', array('main_avto'), array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'district', 'taxonomy general name' ),
'singular_name' => _x( 'district', 'taxonomy singular name' ),
'search_items' => __( 'Search' ),
'all_items' => __( 'All district' ),
'menu_name' => __( 'district' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '/', 'with_front' => false ),
));
} 发布于 2021-01-11 00:41:01
当你尝试任何东西的时候,添加一个自定义的帖子类型,或者一个自定义的分类法...等,您应该始终使用flush_rewrite_rules()重置重写规则。
删除重写规则,然后重新创建重写规则。此函数在与自定义post类型一起使用时非常有用,因为它允许自动刷新WordPress重写规则(对于新的自定义post类型,通常需要手动完成)。
邮箱:的
将以下内容添加到function.php文件的顶部。它应该可以解决你的404.php错误问题。
<?php /**
* flush_rewrite_rules( bool $hard = true )
* Remove rewrite rules and then recreate rewrite rules.
* $hard (bool) (Optional) Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
* Must be removed before pushing to live.
* @link https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
*/
flush_rewrite_rules(); ?>发布于 2021-07-29 11:01:21
在类似的问题上,将register_taxonomy()移到register_post_type()之前作为suggested here帮助我解决了这个问题。
https://stackoverflow.com/questions/65655288
复制相似问题