【问题标题】:Custom taxonomy return 404, only last create taxonomy 200自定义分类返回 404,仅最后创建分类 200
【发布时间】:2021-07-29 19:01:20
【问题描述】:

我有自定义帖子类型和此自定义帖子类型的 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 ),
    ));

} 

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    当您使用任何东西时,添加自定义帖子类型或自定义分类...等。您应该始终使用flush_rewrite_rules() 重置重写规则。

    删除重写规则,然后重新创建重写规则。此功能在与自定义帖子类型一起使用时很有用,因为它允许自动刷新 WordPress 重写规则(对于新的自定义帖子类型通常需要手动完成)。

    将以下内容添加到您的 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(); ?>
    

    【讨论】:

    • 您是否在分类中添加了术语?在 Wordpress 中,自定义分类法没有默认的分类法索引页面。您总是必须根据分类搜索术语。例如:domain.com/taxonomy/ = 404.php ... 但 domain.com/taxonomy/term/ = taxonomy.php 或 archive.php ... 等。不久前发了一篇关于这里的帖子stackoverflow.com/a/63907282/3645650
    【解决方案2】:

    在类似的问题上,将register_taxonomy() 移动到register_post_type() 之前作为suggested here 帮助了我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2020-03-22
      • 2010-09-25
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多