I have custom post type called books
. This has a category taxonomy, called book_types
. So, for example:
Books -> Fiction -> Da Vinci Code
- Custom Post Type: Books
- Custom Taxonomy: Book Types (fiction, non fiction, etc)
- Single entry: Da Vinci Code
I have the permalinks structure to be:
/books/fiction#da-vinci-code
All the books are listed in their taxonomy category page as shown above. Da Vinci Code is just one part of the page for Fiction... as seen from the #da-vinci-code
hashed value in the URL.
This means the taxonomy template is the important one. So I have a template:
template-book_types.php
But this doesn't work. WP gives a 404. What am I missing? My code to create the CPT and Taxonomy is below. (And yes, I have flushed the Permalinks many times.)
/**
* 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 );
archive-books.php
, but even so,/books/fiction
would not show 404 error if there's a post withfiction
as the slug, in yourbooks
CPT.archive.php
orsingle.php
. In this case, why does it work with one language and not with the other?fiction
won't be a post in books, it's the category (book_types
taxonomy).fiction
, then/books/fiction
would have not shown a 404 page, becausebooks
is the rewrite base for your CPT. Also, if/book_types/fiction#da-vinci-code
doesn't work, then try/en/book_types/fiction#da-vinci-code
or whatever the correct language-based path set via Polylang. (and note that the/en/
isn't the "prefix" mentioned in my revised answer)