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.