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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// *-------- Let's Make a Custom Post Type! -----------*/ function my_testimonials() { // Set's all of the dashboard language $labels = array( 'name' => 'Testimonials', 'singular_name' => 'Testimonial', 'add_new' => 'Add New', 'add_new_item' => 'Add New Testimonial', 'edit_item' => 'Edit Testimonial', 'new_item' => 'New Testimonial', 'all_items' => 'All Testimonials', 'view_item' => 'View Testimonial', 'search_items' => 'Search Testimonials', 'not_found' => 'No Testimonials found', 'not_found_in_trash' => 'No Testimonials found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Testimonials' ); //The important settings for building out CPT $args = array( //recall the $labels we just set above 'labels' => $labels, 'public' => true, 'rewrite' => array( 'slug' => 'testimonials' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ) ); //Register that post type with all of your above settings, FUN! register_post_type( 'Testimonial', $args ); } //initialize your action add_action( 'init', 'my_testimonials' ); |
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:
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.***
[…] Head on over to Part 2 – the fun stuff […]
[…] a really great collection of hooks that are well documented and catalogued. In fact, a bunch of my tutorials here use WordPress […]