Custom Post Types (Part 2)

So this is Part 2 of the series on building a testimonials section on your website using Custom Post Types. If you haven’t already read through Part 1 please do. It will give you a good amount of information you need to be able to take this tutorial and apply it to any custom post type you need. The first part is really the most important part and the step that everyone seems to mess up.

Let’s get started!

The first thing you need to do in order to get your custom post type to show up on the left side of your WordPress admin dashboard is to register your post type.

At it’s core, it’s really just this:

<?php register_post_type( $post_type, $args ) ?>

But in reality, it is so much more. When registering your post types you want to think of all of the implications that your post type will have. As covered in Part 1, you need to have planned out how you will use your post type as the $args part of this has a bajillion options for you to play with! Have a look at the codex page and soak all of that in. Everything under the “Arguments” heading is basically a setting that you can set for your CPT.

Below is the entire chunk of code you need to register your post type. Read it. Understand it. Absorb it. This code will be pasted into your functions.php

A recap of your $args

A lot of these are pretty self explanatory, so I will only cover the more confusing ones. You can check out the codex page to see more about all of these.

  • labels
    This is an array of all of your language options for displaying the CPT in the dashboard. You do not have to set all of these because WordPress will just give you the basic UI with your CPT’s name and leave it out where a plural is needed. It is uber useful when the singular of your CPT is different from the plural. Such as MOUSE and MICE.
  • public
    This option will basically set your post to be public and available to the front end. If you want to fine tune the public availability you can add in more args, but exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are all inherited from this.
  • menu_position
    This defines where on the side menu in your dashboard your CPT will show. Setting it to 5 will show it below the Posts menu item. Setting it to  less than 5 will show it above them. You can see where all of the priorities will place your CPT in the codex here.
  • supports
    This is a fun option to play with and you have to make sure you set it otherwise you will be missing some key features as title and editor are the only ones on by default. We want to enable the title, editor, the post thumbnail (aka featured image), and excerpt.
  • has_archive
    When this is set to true, WordPress will automagically create a rewrite rule for you that will create a list of all of your testimonials similar to how you have a list of all of your blog posts. You can access it immediately by going to domain.com/testimonials

Your dashboard should now look like this:

Registered custom post type with all args

 

Once your testimonial is added, you can save and view the testimonial and it’s archive page. Isn’t that neat? So far you haven’t had to do anything on the front end, yet your testimonials are already showing up in your theme just like WordPress posts do.

Oh? You went to it and got a “404 Not Found”? It’s ok. When you make changes to any rewrite rules (such as adding a custom post type) you often have to go to settings -> permalinks in your dashboard and click update without changing anything. This is called flushing the rewrite rules.

Happy Fun Times!

That’s it for Part 2. You now have a functioning Custom Post Type which you can add content to and display on the front end of your website complete with custom rewrite rules!

***Coming up next we will cover custom taxonomies & custom meta boxes for your newly minted Testimonials Custom Post Type.***

The_Idiot

About Lindsay

Self made millionaire.. Oops I mean web developer.. who started out learning how to build websites with tables and dreamweaver, I now almost entirely focus on CMS-based development, usually WordPress. I love coding and designing and regularly switch between my left and right brains.

2 thoughts on “Custom Post Types (Part 2)

  1. […] Head on over to Part 2 – the fun stuff […]

  2. […] a really great collection of hooks that are well documented and catalogued. In fact, a bunch of my tutorials here use WordPress […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">