In this post, I’m going to talk about custom post types in WordPress – a very handy bit of knowledge to become familiar with. If you’ve ever wanted to add a custom section of your WordPress website, this post will benefit you. And the thing is, while setting up custom posts and custom archive pages is kind of confusing to begin with, it’s really easy to get the hang of. Once you understand the parts, the challenging areas of this section of your site will be a thing of the past.
Custom Post Type UI
Let’s start off with the primary plugin you’ll need to connect the dots of your custom posts and archive pages. The plugin is called, “Custom Post Type UI” and you can download it here from the WordPress plugin directory and install it manually or you can simply search for it from your plugin area of your WordPress install. Either way, once it’s installed, there are only a few settings you need to be aware of (to get the ball rolling).
After you get the plugin installed, you should see a menu over to the left that looks like this (click for larger image):
There are a few areas of the setup that need attention. If you click the “Add/Edit Post Types” link in the menu I just described above, you’ll see some fields that need to be filled in.
First, fill in the post type slug. This has the capability of being part of the URL that holds the posts and archive page, so be short, but descriptive. Also, be sure to follow the directions while creating the slug. There are a few rules to follow (example: gallery).
Second, fill in the plural label. This is going to be used as a menu item in the WordPress admin area, so again, keep it short, but descriptive (example: Galleries).
Third, fill in the singular label, which will be used as a heading while you are creating and editing your custom posts. (example: Gallery Section).
Next, there are some more advanced areas that need to be set over to the right. In previous versions of this plugin, these were held below the initial settings and were accessed via a link. In the most recent version, all you have to do is click the arrow to the right of the word, “Settings.”
The first attribute you need to set is the “Has Archive” setting. If you set this to true, WordPress will create a custom archive page, specifically for your custom posts. So, if you are creating some sort of a gallery or portfolio with many custom posts, the custom posts can show on a custom archive page. This is probably the most commonly desired option. Also, you have the option of creating a custom slug for the archive page. If you leave this blank, the post type slug will be used.
It’s important to remember that if you do request that an archive be used, either the archive.php or the archives-$posttype.php template file will be used.
The other attribute that needs attention is called, “Rewrite.” This is coupled with “Custom Rewrite Slug.” If you set Rewrite to true, WordPress will “rewrite” your URL path to work with your custom posts. With the Custom Rewrite Slug, you can set this URL to look like whatever you want.
After all of these areas are set, you should be able to create a new custom post by using the menu to the left in the WordPress admin. So, let’s say you created a label called, “Gallery,” you could mouse over the Gallery link on the left and click the “Add Gallery Section” link. This will bring you to the add post page. Once you add a custom post, you should see the automatically generated slug.
Let’s say, for example, you create a custom post called, “My First Work of Art” and saved it. You should see the slug as “http://www.example.com/gallery/my-first-work-of-art/. If you copy just the beginning part of the slug, for example, http://www.example.com/gallery/, and paste it into a browser, you will see the archive page that holds the custom posts.
In order to add a link to the custom archive page, you’ll need to visit the “Appearance > Menus” section, where you can set up a new “Custom Link.” Once you create the link, add it to the menu you’d like to see it in on your site.
Again, if you don’t have a custom archive page set up specifically for this area of your site, WordPress will use the standard archive.php template for it. If you wanted to create a custom archive page for the, for example, Gallery section, you’d have to create a template file named, archive-gallery.php.
In order for you to have your custom posts listed on a page, such as the archive page, you don’t necessarily need to follow the latter instructions above. You can just as easily not turn on the “Has Archive” setting and go ahead and create a custom page, like I described in an earlier post. If you have a custom page template set up, while creating your custom page in the admin, you would choose the specific page from the “Template” drop down in the “Page Attributes” box. Also, inside the code of the custom page template, you’d add some code to the loop that looks like this:
<?php $args = array( 'post_type' => 'gallery' ); $the_query = new WP_Query( $args ); ?>
This would call the custom posts in this category to this custom page.
Finally, we’re going to talk about how to set up an actual custom post template to use with your custom post types. After all, you most likely want these posts to be unique from other posts on your site.
If you look at the WP Hierarchy, under Singular Page > Single Post Page > Custom Post, you’ll see that you have the ability to create a custom post template for these pages. The naming convention for these custom post templates looks like this: single-$posttype.php. If you don’t create this file, the generic fallback single.php will be used. In your custom post template, you can add whatever code you’d like.
I hope I helped you create your custom post types on your website. If you have any questions or comments, please leave them in the comment section below.