【问题标题】:Wordpress - Archive Custom Post Type just works to default permalink configurationWordpress - 存档自定义帖子类型仅适用于默认永久链接配置
【发布时间】:2015-10-31 04:18:47
【问题描述】:

我正在构建我的第一个插件,它包含一个名为“功能”的自定义帖子类型 (CPT)。我正在尝试访问此 CPT 的“存档”页面,但使用除默认配置之外的任何永久链接配置时出现“错误 404”。

当我对永久链接使用“默认”配置时,返回的“存档”来自模板,而不是来自我的插件。我做错了什么?

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,
    )
 );
}

【问题讨论】:

    标签: wordpress custom-post-type archive permalinks


    【解决方案1】:

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

    引自 WordPress。

    has_archive(布尔型或字符串)(可选)启用帖子类型存档。
    默认情况下将使用 $post_type 作为存档 slug。默认值:假

    注意:

    如果启用了重写,将生成正确的重写规则。也使用 重写以更改使用的 slug。

    所以你的注册帖子类型数组将是

    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',
           ),
    
      )
    );
    

    【讨论】:

    • Noman,谢谢你的回答。你完全正确!当我将“has_archive”定义为 true 时,它​​可以完美运行。
    • @AndréRocha 如果对您有帮助,请接受我的回答?
    猜你喜欢
    • 2012-11-16
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2012-02-22
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多