Using Custom Post Types (CPTs) in WordPress we can create our own custom content types besides the built-in Posts and Pages. Similarly custom taxonomies can be created similar to the built-in categories and tags. And then there are custom fields which can be attached to a CPT to set that content-type specific features/attributes.
In this article I go over registering a Books CPT, registering a Genre taxonomy to categorize the books, adding a custom field group consisting of Price, Author, Published Year and Purchase Link. I then share the templates, archive-books.php and single-books.php which determine how the Archive (list of books) page and individual book pages are displayed.
Note that he code can be used with any CPT, taxonomy and custom fields in Genesis if you understand how the entire solution is put together and operates. I have added comments in the code to make it easier for beginners.
Books CPT Archive page:
Books CPT Single page:
Summary
- Custom Post Type and the custom taxonomy are created using Types plugin.
- Custom fields are created using Advanced Custom Fields plugin.
- Genre taxonomy term archives are set to use Books CPT archive template.
- Display Post meta only if the entry has been assigned to at least one Genre term
Dashboard
- Archive Settings option is added to Books CPT after which Books > Archive Settings menu item/page becomes available.
- List of Genres that are attached to Books will be displayed in a column.
CPT Archive template
- URL: http://example.com/books/
- Uses full width layout.
- Headline and Intro Text entered in the backend will appear above the list of Books.
- In each Book, entry Title and custom fields' values appear at the left. Excerpt and featured image (Medium size) at the right. If the Book has been assigned to any Genre, it will appear below. Multiple Genres will appear comma separated.
CPT Single entry template
- Sample URL: http://example.com/books/nate-the-great-where-are-you/
- Breadcrumbs (if enabled for Posts in Genesis theme settings) will appear above the Book title above the content sidebar wrap.
- Medium size featured image (if present) appears at the left with the Book entry's content floating around it.
- If the Book has been assigned to any Genre, it will appear below the content. Multiple Genres will appear comma separated.
- Custom fields (whichever have values) appear above the right sidebar.
- Any widgets placed in a custom 'Primary Sidebar - Book' widget area will appear in place of the default Primary Sidebar in the right.
- Links to Previous and Next Books are shown below the content sidebar wrap.
Step 1
Install and activate Types.
At Types > Custom Types and Taxonomies, add a new CPT like so:
Plural name: Books
Singular name: Book
Slug: books
Tick Thumbnail.
Step 2
Add a new custom taxonomy like so:
Plural name: Genres
Singular name: Genre
Slug: genre
Registered post types that will be used with this taxonomy: Book
Type: Hierarchical - like post categories.
Step 3
Install and activate Advanced Custom Fields.
Go to Custom Fields > Custom Fields and create a new group called 'Book Details'.
Summary:
Step 4
Go to Books > Add New and add your CPT entries.
Step 5
Add the following in child theme's functions.php:
To view the full content, please sign up for the membership.
Already a member? Log in below or here.
Very useful tutorial explained in most simple manner!
Now I am addicted to your website.. Thank you very much for your great tutorials..
Really great tip Sridhar! You have the best Genesis Tutorials.
Great, i want to ask a question, is it possible to use the default sidebar in both templates?
Another thing, can i use it to get a deal page like that of http://www.wpbeginner.com/deals/ or http://www.techlila.com/coupons/
Fantastic post Sridhar, If I had seen this a couple years back it would have saved me hundreds of hours building bviyachtcharters.com. Check out the Catamarans and monohull, their archives, and custom post pages.
Hi Ridhar Katakam
I’m building my site with Genesis 2.0.2 and element40 child theme. I add code and include manually in coupon-posttype.php for ‘coupon’ custom post type and ‘Coupon type’ taxonomy. I also add support for ‘genesis-cpt-archives-settings’ in custom post type definition. My setting in Permalinks is custom with value is: /%category%/%postname%/. I added archive-coupon.php and single-coupon.php with some code. But I couldn’t use the link like this: http://www.example.com/coupon to access to list of all coupon post so I couldn’t access single coupon.
Could you show me why
Thanks so much
Thanks for the great tutorial Shridhar
i am not seeing the WIDGET area on the Single Book Detail page
would you have any ida how I can fix this please?
thanks
Phil
Thank you Sridhar
I want to make different single post to one category.
Hi Sridhar,
thank you, that helps a step further. It’s not exactly, what I am searching for, How I can add a second term?
Thanks in advance
Angelika
hello
First nice tutorial, love it…..
I have follow this tutorial to the smalest .,+/” and I can´t get the genre to show in the single page..
I see it on the archive page…
What can i do….
Niklas
Thank you for the tutorial, Sriddhar:
I am having one problem though: On the archive page, it is displaying the entire content of the book instead of the excerpt.
Could you (or anyone else reading this), please tell me what I might be doing wrong? I have double checked the copied and I copied and pasted it exactly as you have published it here.
Can you tell me the exact code in the sample you gave that would be responsible for displaying the excerpt in the archive-books.php page?
Thank you so much.
Ooppsss, I should clarify one thing…
When I say that that “it is displaying the entire content of the book instead of the excerpt,” what I really mean is that I want to display the text that I put into the EXCERPT META BOX (I do not mean just truncated content box content.)
So I would appreciate if if there is a way to check if the excerpt meta box has content, and if not, then show the truncated content.
Thanks so much.
“I am having one problem though: On the archive page, it is displaying the entire content of the book instead of the excerpt.”
I had this problem too, but solved it by the main Genesis settings – changed Content to Excerpt.
Hope this helps. I’m sure there is a way to do it just one the one page, but I don’t yet know it.
Brilliant tutorial. Thank you so much. Just what I was looking for and used in my new site. One little note if you don’t mind me adding. I may have missed it in your post but to get the sidebar to work I needed to add
add_post_type_support( ‘your_custom_post_type’, ‘genesis-layouts’ );
to my functions.php
I hope that helps anyone with the layout options not appearing on their CPT
Thanks again. Your tutorials are brilliant and extremely useful.
Hello,
Hope Sridhar or anyone here can offer help. I followed the tutorial strictly but using my own variables. However, the custom fields are not showing up on the front end. Also, it seems the archive templates are not be called.
Thanks again for the tutorial.
Thanks for the tutorial, how would I add a sidebar to the archive page?
Delete these lines in archive-books.php: http://pastebin.com/raw.php?i=zjryrHkK
The archive page will then use the default layout as set in your Genesis theme settings.
Thanks so much for this! Exactly what I needed! I have one more need, though, and I’m wondering how to do it. Imagine that you want posts in a “kids-books” category to have very different styling than posts in a “mystery-books” category. This would include different images, colors, etc. What would be the best way to go about doing this?
body
element of each category archive page will automatically have a class that you can use to style elements on that page.For example, “kids-books” category page will have this as one of the body classes: category-kids-books
So you can prefix your elements’ classes with this like this:
.category-kids-books .entry {
background: #ddd;
}
Sridhar Katakam , any chance we can hide certain fields (ex. the button) on the archive pages?
Thanks
Stijn
Thanks so much for this tutorial! I am running into an issue with the meta data. The post date is displaying above the title and the published date is not displaying. Thanks!
Same here…
If you would like to display the published date
in the CPT archive: in archive-books.php replace http://pastebin.com/raw/GkYimW4H with http://pastebin.com/raw/ZAWkMCc8. http://d.pr/i/14Odq
in the CPT single entry pages: in single-books.php delete
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
http://d.pr/i/W4fa
Just got a membership to utilize this tutorial, but I have a few issues.
Could you please add a force layout function to the single template? The sidebar does not display if Genesis settings have a full width default layout.
Also, on that note, when I can get the sidebar to display, it’s also showing the Primary Sidebar widget area, which is not desired.
Thanks for this tutorial
Good point. I have added the line of code for this in the single CPT template.
Does not happen in my test site. Is Genesis Simple Sidebars active by any chance?
I’m getting the same problem as jorwesflow above. The Primary Sidebar widget area is showing up instead of the one in the tute. I’ve done the tute 3x with the same issue. Can someone look into this. I am using the Executive Pro theme. Thanks
Are you using Genesis Simple Sidebars plugin?
Sridhar
Thanks for the reply and yes I am using simple sidebars. Are you psychic? lol
Haha, no.. I have seen this problem before when that plugin is active, just experience coming into play here.
Try this: In the template for single CPT entries, change http://pastebin.com/raw/QZ15ysYF to http://pastebin.com/raw/g4YYH6T5.
Sridhar
Thanks again for your help! The code worked and took away the primary sidebar. Another issue I have is I don’t see the two custom fields below on the archive and single page. I’ve gone over this tutorial inside and out and cannot figure this out what’s the problem.
1. Book Publisher Year
2. Book Publisher Link
The price and author custom field are showing correctly on the archive and single page.
I created the four “Custom Fields” exactly as you have them in the tutorial but only two are showing.
Thanks in advance.
Works fine for me on my test site.
Single CPT entry page: http://cl.ly/373T2g2X0P0C
CPT archive page: http://cl.ly/1F1U190d0r0h
Can you provide the URL of your site?
Sridhar
Thanks for getting back to me. I’m going to hold off on this tutorial for now. Thanks again!
Hi Ridhar, i want to apply this to a car dealer web so, how can I create a search filter for the custom fields? For example, searching for a year and price?
Thanks for all
You may want to use one of the plugins like FacetWP or Search & Filter Pro or Genesis Filter Everything Deluxe Combo.
https://sridharkatakam.com/go/facetwp/
http://www.designsandcode.com/wordpress-plugins/search-filter-pro/
https://sridharkatakam.com/go/genesis-filter-everything-deluxe-combo/
Ok, thanks. I’ll try that solution.
How can I show the list of genres in sidebar?
Is the Template Redirect code near the bottom of functions.php still accurate?