Wordpress - 存档自定义 Post 类型仅适用于默认永久链接配置

Wordpress - Archive Custom Post Type just works to default permalink configuration

我正在构建我的第一个插件,它包含一个名为 "feature" 的自定义 post 类型 (CPT)。我正在尝试访问此 CPT 的 "archive" 页面,但我使用除默认配置之外的任何固定链接配置得到 "error 404"。

当我对永久链接使用 "Default" 配置时,返回的 "archive" 来自模板,而不是我的插件。我做错了什么?

function fmp_create_post_feature() {
register_post_type( 'feature',
  array(
      'labels' => array(
          'name' => 'Features' ,
          'singular_name' => 'Feature',
          'edit_item' => __( 'Edit' ) . ' Feature',
          'add_new' => __( 'Add' ) . ' nova',
          'add_new_item' => __('Add').' nova Feature',
          'menu_name' => 'Feature with Modal Popup',
          'all_items' => 'Features',
          'rewrite' => array( 'slug' => 'feature' ),
      ),
      'public' => true,
      'menu_icon' => 'dashicons-desktop',
      'supports' => array(
          'title',
          'editor',
          'thumbnail'
       ),
       'taxonomies' => array(
          'feature',
       ),

  )
);
flush_rewrite_rules();
}

add_action( 'init', 'fmp_create_post_feature' );

上面的代码是cpt注册,下面的代码是分类注册

add_action( 'init', 'fmp_create_tax' );

function fmp_create_tax() {
 register_taxonomy(
    'feature',
    array(
        'label' => 'Feature',
        'rewrite' => array( 'slug' => 'feature' ),
        'hierarchical' => true,
    )
 );
}

这是因为您在注册自定义 post 类型时没有定义 'has_archive' => true,

引自 WordPress。

has_archive (boolean or string) (optional) Enables post type archives.
Will use $post_type as archive slug by default. Default: false

注:

Will generate the proper rewrite rules if rewrite is enabled. Also use rewrite to change the slug used.

因此您的寄存器 post 类型数组将是

register_post_type( 'feature',
  array(
      'labels' => array(
          'name' => 'Features' ,
          'singular_name' => 'Feature',
          'edit_item' => __( 'Edit' ) . ' Feature',
          'add_new' => __( 'Add' ) . ' nova',
          'add_new_item' => __('Add').' nova Feature',
          'menu_name' => 'Feature with Modal Popup',
          'all_items' => 'Features',
          'rewrite' => array( 'slug' => 'feature' ),
      ),
      'public' => true,
      'has_archive'        => true,
      'menu_icon' => 'dashicons-desktop',
      'supports' => array(
          'title',
          'editor',
          'thumbnail'
       ),
       'taxonomies' => array(
          'feature',
       ),

  )
);