最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to define the template for custom posts?

programmeradmin2浏览0评论

I created a new custom type called 'books' and I created a Field using the Advanced Custom Fields plugin where I define the fields and create the records, with post names such as book1, book2, etc. I want to define a view for the elements of this particular custom type so when the visitor goes to website/books/book1 I will display information about book1. I tried defining single-books.php at the root of the theme but it didn't work. How can I define a template for the posts of a custom post?

Thanks

I created a new custom type called 'books' and I created a Field using the Advanced Custom Fields plugin where I define the fields and create the records, with post names such as book1, book2, etc. I want to define a view for the elements of this particular custom type so when the visitor goes to website/books/book1 I will display information about book1. I tried defining single-books.php at the root of the theme but it didn't work. How can I define a template for the posts of a custom post?

Thanks

Share Improve this question asked May 22, 2019 at 16:32 GonzaloGonzalo 1672 silver badges10 bronze badges 5
  • single-books.php would be correct for a CPT named books. However you may want to double-check your code and adjust so the name is book singular. When you say you added the file and it "didn't work," what happened? Did you confirm that a different template was being used? – WebElaine Commented May 22, 2019 at 17:07
  • Also, can you clarify your setup - are you actually creating individual "Book" posts in WP? From the way you worded your scenario it sounds like you might be trying to create just custom fields without associating them to a post. You'll need to create each actual "Book" post and you can then use ACF to add metadata for that post, and creating those posts is what actually allows you to go to example/books/book1 - it relies on having an actual post with the slug "book1". – WebElaine Commented May 22, 2019 at 17:09
  • @WebElaine When I tried to access /books/name of post I got page not found. Any idea why? – Gonzalo Commented May 23, 2019 at 7:00
  • @WebElaine regarding the second comment. Yes I created all the posts and their respective metadata using ACF. – Gonzalo Commented May 23, 2019 at 7:01
  • After you registered your post type, did you visit the Settings > Permalinks page? This flushes permalinks and makes new permalinks work - you don't have to change any settings, but you do need to visit that page or use code to flush the rewrite rules. – WebElaine Commented May 23, 2019 at 13:49
Add a comment  | 

1 Answer 1

Reset to default 1

You can Use single-{posttype}.php for the single template.

And, if you have register your post type with the has_archive argument set to true, then you can use archive-{posttype}.php for your archive template.

You can check Template Hierarchy

Also for 404 Page, double check that page slug and post-type slug is NOT SAME. I mean check whether you have created any page with slug "books".

发布评论

评论列表(0)

  1. 暂无评论