Most custom post types you add to your site will work like posts, which means they’re designed to be displayed in archive pages. Maybe you’re using a custom taxonomy to display them, or maybe you’ve added the “category” taxonomy like I have.
Alternatively, if you’ve used a plugin like WooCommerce that adds custom post types in order to add specific functionality to your site, then that plugin might add some specific pages (like the “Shop2 page) designed to output your custom post type.
The four options you have for displaying custom post types are:
- Displaying the individual post (i.e. the book, in my example)
- Displaying the full post type archive, with all of the books listed.
- Displaying a custom taxonomy archive, using a taxonomy you’ve registered for your custom post type.
- Integrating posts from the custom post type (e.g. books) into the archive for an existing taxonomy such as ‘category’, or into the main blog page.
Let’s take a look at each of these in turn.
Warning! If at any point in this process you can’t access the archives or individual posts for your custom post type, its because WordPress hasn’t yet written the permalinks for them. To fix this, go to Settings > Permalinks and just click the Save Changes button. This will tell WordPress to check for any new post types and create the URLs for them.
The first option you have is to create an archive page of all the posts in your custom post type archive.
If you defined ‘has_archive’ as true when you were registering the post type, WordPress will automatically generate an archive page for your post type. You can add this to your navigation menu using the Customizer.
Open the Customizer and select the Menus option. Select your main menu and click the Add items button to add extra items to your menu.
Select your post type from the options that appear and click not he downwards arrow to the right of its name. This will display al list that includes each of the posts you’ve added, plus an option which in my case is called All Books. Click on that and it will be added to your menu.
If you then click on that item in the menu, you can edit the label. I prefer not to call mine “All Books”, but just “Books” instead.
Note that you can only do this via the Customizer – it isn’t possible in the Menus screen. So make sure you use the Customizer to add your custom post type archive to your menu.
WordPress will use the template hierarchy to identify which template file in your theme to use to display the custom post type archive.
If your theme has an archive.php file, it will use that, and if it doesn’t, it will use index.php.
If you want to edit the way that the custom post type archive is output, then you can create a template file for your custom post type archive.
For a specific custom post type, you need to create a template file called archive-$posttype.php, where $posttype is the name of your post type. So for my books post type, I’d create a file called archive-kinsta_book.php.
The easiest way to create this file is by making a duplicate of the archive.php file in your theme. Rename it and edit it so it displays your post type archive the way you want to.
Single posts created using a custom post type will also be shown using the first relevant template file that’s found in the template hierarchy.
You can go a bit further with single posts than you can with archives: not only can you create a template file for the post type (single-$posttype.php), but you can also create a file for a specific post of that post type using the slug for that post (single-$postype-slug.php).
So if I added Great Expectations to my book reviews site, I could create a file for all book reviews called single-kinsta_book.php, or I could create a target file for that book called single-kinsta_book-great-expectations.php. If I didn’t create either of those files, WordPress would default to using single.php or (if that didn’t exist) singular.php or index.php.
« How to Display the Title for the Posts / Blog Page in WordPress
Making Conditional Tags Work Magic in Your WordPress Site »