我有名为books的自定义post类型。这有一个类别分类法,称为book_types。因此,例如:
->小说->达·芬奇密码
我的permalinks结构是:
/books/fiction#da-vinci-code 如上文所示,所有书籍都列在其分类类别页面中。达芬奇密码只是小说页面的一部分.从URL中的#da-vinci-code散列值中可以看出。
这意味着分类法模板是重要的模板。所以我有一个模板:
template-book_types.php 但这不管用。WP值为404。我遗漏了什么?下面是我创建CPT和分类法的代码。(是的,我已经洗过很多次了。)
/**
* Post Type: Books.
*/
$labels = [
"name" => __( "Books", "custom-post-type-ui" ),
"singular_name" => __( "Book", "custom-post-type-ui" ),
];
$args = [
"label" => __( "Books", "custom-post-type-ui" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"can_export" => true,
"rewrite" => [ "slug" => "books", "with_front" => true ],
"query_var" => true,
"menu_position" => 5,
"menu_icon" => "dashicons-schedule",
"supports" => [ "title", "editor" ],
"taxonomies" => [ "book_types" ],
"show_in_graphql" => false,
];
register_post_type( "books", $args );
/**
* Taxonomy: Book Types.
*/
$labels = [
"name" => __( "Book Types", "custom-post-type-ui" ),
"singular_name" => __( "Book Type", "custom-post-type-ui" ),
];
$args = [
"label" => __( "Book Types", "custom-post-type-ui" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => [ 'slug' => 'book_types', 'with_front' => true, ],
"show_admin_column" => true,
"show_in_rest" => true,
"show_tagcloud" => false,
"rest_base" => "book_types",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
"sort" => false,
"show_in_graphql" => false,
];
register_taxonomy( "book_types", [ "books" ], $args );发布于 2022-04-05 07:22:30
分类法的rewrite基础实际上是book_types (请参阅代码中的"rewrite" => [ 'slug' => 'book_types', 'with_front' => true, ] ),因此在/book_types/路径上可以访问分类法存档页面。而不是/books/。因此,如果/books/fiction显示一个404错误页面,这是正常的,当然,如果有一个带有fiction片段的帖子,则例外。
其次,请注意,'with_front' => true意味着如果您在permalink Settings管理页面上的Custom结构设置使用类似于/blog/的前缀(不管是否带有前导/第一个斜杠),那么您的分类法的术语在Permalink中也会有这个前缀,例如https://example.com/blog/book_types/fiction/。
同样也适用于将with_front设置为true的post类型。例如,https://example.com/blog/books/da-vinci-code/将成为"Da Vinci Code“帖子的permalink。
因此,对于问题中的404错误,请尝试加载/book_types/fiction#da-vinci-code,并检查是否看到了正确的分类法存档。
另外,您应该知道,taxonomy归档的模板名实际上应该是这样的:taxonomy-.php --或分类法中的特定术语的taxonomy--.php,因此您应该将模板从template-book_types.php重命名为taxonomy-book_types.php,以便WordPress能够正确地将其加载到分类法归档文件中。
有关更多详细信息,请参阅https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/。
https://wordpress.stackexchange.com/questions/404442
复制相似问题