自定义分类模板列表不起作用(404)

时间:2019-11-11 作者:sariDon

我已创建自定义帖子类型“item”:

function mywp_add_item_post() {
    $labels = array(
        \'name\' => \'Items\',
        \'singular_name\' => \'Item\',
        \'add_new\' => \'Add New Item\',
        \'add_new_item\' => \'Add New Item\',
        \'edit_item\' => \'Edit Item\',
        \'new_item\' => \'New Item\',
        \'all_items\' => \'All Items\',
        \'view_item\' => \'View Item\',
        \'search_items\' => \'Search Items\',
        \'not_found\' =>  \'No Items Found\',
        \'not_found_in_trash\' => \'No Items found in Trash\', 
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Items\',
        \'show_in_nav_menus\'=>true
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'rewrite\' => array(\'slug\' => \'item\'),
        \'query_var\' => true,
        \'menu_icon\' => \'dashicons-randomize\',
        \'show_in_rest\' => true,
        \'show_in_nav_menus\'=>true,
        \'supports\' => array(\'title\', \'editor\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'thumbnail\', \'author\', \'page-attributes\'));

    register_post_type( \'item\', $args );
  }

add_action( \'init\', \'mywp_add_item_post\');
添加了名为“collection”的自定义分类:

function mywp_add_collection_taxonomy() {
      $labels= array(\'name\' => _x( \'Collections\', \'taxonomy general name\' ),
      \'singular_name\' => _x( \'Collection\', \'taxonomy singular name\' ),
      \'search_items\' =>  __( \'Search Collections\' ),
      \'all_items\' => __( \'All Collection\' ),
      \'parent_item\' => __( \'Parent Collection\' ),
      \'parent_item_colon\' => __( \'Parent Collection:\' ),
      \'edit_item\' => __( \'Edit Collection\' ),
      \'update_item\' => __( \'Update Collection\' ),
      \'add_new_item\' => __( \'Add New Collection\' ),
      \'new_item_name\' => __( \'New Collection Name\' ),
      \'menu_name\' => __( \'Collections\' ));

      $rewrite= array(\'slug\' => \'collection\');

  $args= array(\'hierarchical\' => true,
      \'show_in_nav_menus\' => true,
      \'has_archive\' => true,
      \'labels\'=>$labels,
      \'query_var\' => true,
      \'show_in_rest\' => true,
      \'rewrite\'=>$rewrite);

   register_taxonomy(\'collection\', \'item\', $args);
}

function mywp_tax_filters()
{
    register_taxonomy_for_object_type(\'collection\', \'item\');
}

add_action(\'init\', \'mywp_add_collection_taxonomy\', 1);
add_action(\'init\', \'mywp_tax_filters\');
Object: 我需要一个页面来查看非空的“集合”列表(如默认类别页面)

分类法模板文件(taxonomy collection.php)在主题目录中创建Problem:链接“$site\\u url/collection”显示404(未找到)。

提前感谢您对我的问题的思考和耐心通读。

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

缺少模板永远不会导致404错误。模板不是这样工作的。您获得404的原因是您试图访问一个不存在的URL。

{Site URL}/{Taxonomy name}/ 不存在。WordPress在该URL上没有显示任何内容。WordPress模板仅显示帖子,但此URL未指定要显示的帖子。您需要指定要显示的收集术语,然后访问带有该术语的帖子{Site URL}/{Taxonomy name}/{Term slug}. <所有帖子都可以访问{Site URL}/{Post type name}/, 但是{Site URL}/{Taxonomy name}/ 将返回404。

如果要在分类法中显示术语列表,那么唯一的方法是创建一个带有列出所有术语的自定义模板的页面。它不是WordPress中的本机模板类型。

相关推荐

Display Custom Taxonomy names

我正在尝试显示自定义帖子类型的自定义分类法。因此,此分类法特定于此自定义帖子类型。不幸的是,我无法让它们显示出来。这是我在函数中的代码。php注册自定义分类法:add_action(\'init\', \'products_categories\', 0); function products_categories(){ $labels = array (\'name\' => _x(\'Product Categories\',\'taxonomy general nam