AFAIK, the default template naming scheme cannot accommodate both CPT and taxonomies in one template. You would still need to modify the query of one or the other.
When you request mywebsite.com/books/adventure, WP will try to find a CPT books post whose slug is “adventure”, it does not know this is a category. You need to add a custom rewrite rule so WP knows adventure is a category and not a post.
See https://codex.ww.wp.xz.cn/Rewrite_API/add_rewrite_rule
But now that you’ve done that, you cannot request book posts by title slug, only category! You need another rewrite rule and permalink scheme for WP to know you want a book post and not a category. Perhaps mywebsite.com/book/adventures-of-huck-finn? Note the singular “book”.
This actually backwards, sorry, I didn’t think my post through. I’m not rewriting it 😉 Setup your CPT to use “book” as the rewrite rule. Then add a rewrite rule to identify book categories with the permastruct element “books”. Alternately, create a taxonomy that uses “books” as the element (can be labeled anything else, “Genre” or whatever). Then WP will create the rewrite rule for you.
No matter what you do with rewrites, visit the permalinks settings screen after adding your code to get WP to flush the current rules and create a new set that includes your code.
Joy
(@joyously)
It’s difficult to tell whether you are asking about what the template name should be or how the permalinks work by default.
For a book post type, you would use a different taxonomy name than category. Category is already used for posts. So call yours something else. If you called it genre, the permalink would look like “http://mywebsite.com/genre/adventure”, and the fact that those are books is assumed because that’s what the taxonomy is for. You don’t have to put it in the link.
To see what template file handles each request, see the Template Hierarchy.
https://developer.ww.wp.xz.cn/themes/basics/template-hierarchy/
Hi
here is the code just paste the template part and it will work
<?php
$args = array( 'posts_per_page'=>-1,'post_type' => 'Books','category_name' => 'Adventure' );
$allposts= get_posts( $args );
if ($allposts) {
foreach ( $allposts as $post ) {
setup_postdata($post);
?>
<div class="post_li">
<h3><?php the_title(); ?></h3>
<p><?php ?></p>
<?php $catename= get_the_terms(get_the_ID(),array('country'));
foreach ( $catename as $term ) {
$term_link = get_term_link( $term, array( 'country') );
?>
<a>"><?php echo $term->name ?></a>
<?php
}
?>
</div>
<?php
}
}
?>
check the post type case sensitive
any doubt check the link
https://codex.ww.wp.xz.cn/Class_Reference/WP_Query
You need to coorrect template part path here it is
create a template part books.php
template name-books
then create a page named books select the template books.
now you can see all the posts in the given category.
you want to edit the single view of the posts go to single.php
i hope it works
[ Signature moderated ]
Thanks
-
This reply was modified 7 years, 8 months ago by
ramvijay.
-
This reply was modified 7 years, 8 months ago by
ramvijay.
-
This reply was modified 7 years, 8 months ago by
Steven Stern (sterndata).
-
This reply was modified 7 years, 8 months ago by
bcworkz. Reason: code fixed
@ramvijay – Thank you for helping out! As it happens, the code as you posted it would not work. This is because the forum’s parser corrupted it. I fixed it so it accurately represents what you pasted. In the future, to prevent this corruption, please demarcate all posted code with backticks or use the code button. Also, please do not post signature lines with your reply. Signatures do not conform with the Forum Guidelines.
To anyone else following along as well, I will point out that the posted code does indeed accurately query for posts matching the OP example. However, it will not accurately query other similar requests without altering the code. It’s not suitable for parsing arguments out of URL requests. It meets a specific need, but may not meet the OP’s intent. Only the OP can accurately decide this. As always, any suggestions thought to be helpful are welcome.