You might have noticed that I publish code snippets for the Genesis Framework that developers can use when building their sites.
I choose to build those with custom post types because I didn’t want them to be sent out in my blog feed. While I could have excluded the category from being included in the feed, I also wanted to separate the code snippets in the backend of my site.
For those unfamiliar with custom post types, it is a way of representing content that can be published on your site. By default, WordPress includes 5 custom post types: Posts, Pages, Attachments, Revisions and Nav Menus.
Here’s how I built my code snippets with custom post types…
Registering the Custom Post Type
The first thing I did was register the “code” custom post type in my functions file. Below is the the code that I used for that:
/** Register code custom post type */
add_action( 'init', 'code_post_type' );
function code_post_type() {
register_post_type( 'code',
array(
'labels' => array(
'name' => __( 'Code' ),
'singular_name' => __( 'Code Snippets' ),
),
'has_archive' => true,
'public' => true,
'rewrite' => array( 'slug' => 'code' ),
'supports' => array( 'title', 'editor', 'genesis-seo' ),
)
);
}
You’ll notice there are a number of arguments in the code above, and I’ll explain to you what those are and what parameters were used.
has_archive
This argument allows me to have an archive page for my code snippets. This will be controlled by my archive-code.php file, as shown below.
public
This argument allows my code snippets to be seen by the public. Obviously I want my them to be seen by my readers and indexed by Google, so this is set to true.
rewrite
This argument allows me to set my permalink structure for my code snippet pages. You’ll notice I used the word “code” and that is what shows up in bold in instances like http://www.briangardner.com/code/add-body-class/.
supports
This argument allows me to define the metaboxes that appear on the add/edit “code” page in my dashboard. Obviously I want the title and editor to appear, but I’ve also enabled the Genesis SEO Settings metabox so that I can specify a custom document title or custom page description for the code snippets.
Publishing Code Snippets
Now that I have the “code” custom post type registered, I’m able to start adding code snippets. There’s a top level menu in my sidebar navigation labeled “Code” in the same way there is for Posts and Pages.
I’m able to add new code snippets, as well as edit them from an “edit code” page.
Customizing the Code Page
By default, the “code” custom post type will publish like a blog post, so I want to remove a few elements from the page.
I’ve created a single-code.php file, which looks like this:
<?php
remove_action( 'genesis_before_post_content', 'genesis_post_info' );
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );
remove_action( 'genesis_after_post', 'genesis_do_author_box_single' );
genesis();
Basically I want to remove the Post Info function, the Post Meta function and the Author Box so that they don’t show on my code snippet pages.
Customizing the Code Archive Page
By default, the archive page of a custom post type will show a list of your latest entries in the same way a blog archive page does. Because I wanted a custom archive page for my code, I wrote some code for that.
I’ve created an archive-code.php file, which is hard-coded to display what I want it too. You can see the contents of that file here.
And that my friends is it.
See how easy this is with the Genesis Framework and WordPress?
Now you can register your own custom post type and publish some new type of content. Obviously there are other things you can do with custom post types, but I didn’t need them all.
For more on using custom post types with WordPress, two essential reads are Custom Post Types in WordPress and Custom Columns for Custom Post Types. Those are written by Justin Tadlock, and provide invaluable information.
Thank you for this Brian! I must confess, your timing on this post couldn’t be better….
Just this morning while I was showering I was thinking to myself – “I should make a nice code section on my site for my next redesign. Something like what Brian has.” (Yes, I think about code and WordPress in the shower. That’s normal, right?) I determined that I wold give it a whirl using CPTs the next time I found some free time. But, having 4 kids and a full-time job, I knew that I probably wouldn’t get around to it any time soon. BUT, since you have done all the work for me, I guess I don’t have any excuses.
Thanks Brian – and sorry about the TMI with the shower and all.
Heh, always good to hear that things I do are a) useful to others and b) they are things folks want to replicate and do on their own site. Let me know how this goes for you!
Brian,
This is probably the best write-up I’ve seen on creating CPTs. You’ve broken it down and explained the specifics of it in a way that anyone can understand. Please keep up these great tutorials. They are invaluable to those of us who are not php wizards!
Thanks so much Seth. While my tutorial works great for “code” and my use case, the posts by Justin Tadlock are 5-star quality. That’s pretty much where I learned how to do custom post types.
This is great – very thorough explanation! I’m just learning to use CPTs and I can see that it’s really going to open up a whole new level of business for me to be able to offer that to clients. I just love WordPress.
Dontcha love it?
I’m warning you Dorian, custom post types are somewhat addicting. Once you know how to set them up and use them, you find excuses to create new ones.
I know I’m in trouble because now I’ve started thinking of all sorts of things as “couldn’t that just be done through custom post types?” even when I actually…don’t really know how to do custom post types. But I’m getting there.
Brian,
This is also something I’ve considered adding to my new site…thanks for the excellent writeup (as always!).
I really like the layout of your code snippet page.
Thank you,
Tony
Wow! This is a nice concept!
This would be better than creating those items on a Post by categories, though creating a Custom Post Type has been not easy for as an average level developer, however I can apply this on my personal website ‘Code section’.
This is how I love Genesis and WordPress so much more.
Thanks!
Very awesome. I hadn’t thought of doing that before, but consider me sold on a new way to dev.
Hi Brian,
I love using CPT’s to keep particular posts separate from my regular content.
Justin also posted a link to this CPT Code Generator he found a while ago and I use it all the time for creating them. It saves me a little bit of time.
I thought people might appreciate this resource:
http://themergency.com/generators/wordpress-custom-post-types/
Amelia
Wow, I ran into this issue a few weeks ago to list products for a client (with no ecommerce features). I added it to the (in)Spyr Child theme about 5 minutes ago, and it works like a charm!
Thank you Brian for these informative blog posts, I follow your blog actively and appreciate that there are people like you that share this valuable information.
Awesome Zach, great to hear you have it implemented! Be sure to give Justin’s articles a fine look over, as there is more to CPT than what I explained.
Interesting stuff, but just slightly over my head. It’s like a baby trying to eat a steak sandwich.
Give yourself a little more credit Charles. I’ve seen the sites you’ve built, you’re a toddler at the very least.
Too kind. Too kind……
Thanks for the great info Brian!
I have a “how-to” request:
“How to use the genesis featured posts widget with custom post types”
Hi, Carlos – I’ve used the Featured Widget Amplified plugin and it works great with custom post types. It will let you choose your particular post type via the drop-down menu. (Maybe you already know that but I wasn’t sure if you’d tried the Featured Widget Amplified plugin or just the out-of-the-box Genesis featured posts widget).
@Dorian Speed
Thanks! I’ll give it a try. The genesis widget looks like it would be easy to adapt for a custom post type.
This is perfect.
This morning I needed to run a shortcode in my sidebar which WordPress doesn’t support by default. I discovered that it only takes one line of code in the functions.php to make it happen. I wanted to share this one-liner on my website but didn’t feel it worthy of an entire post. Your custom post type tutorial is exactly what I needed.
Thanks for the great tutorial Brian. I’ll be implementing this functionality very soon.
It’s amazing all that is possible with WordPress and Genesis, isn’t it Rick?
It really is Brian.
I just followed your tutorial and in less than 15 minutes I now have the code as well as my first piece of ‘code snippet’ running on my site.
Thanks for being so generous with sharing your coding secrets!
Thank you for this!
I have recently (very slowly) been putting up codes I have been using from time to time on my site, or codes I want to remember later on my site. I don’t want to send them out, so I have just excluded them from sending. I’ll have to try this, as it just looks so much better.
Awesome tutorial as usual! Thank you for breakin’ it down for us
I’ll definitely be using this in the near future.
Thanks for this one Brian!
I have been wanting to share some of the knowledge you have passed on.
Remind me again. Content is king right?
Yes, it definitely is.
Thanks! I was searching for exactly this!
Hi Brian!
Thank you for sharing very informative and must have article with me especially, I am continuous follower of your blog post and all the time finds great resources. Thanks for sharing great post
Hi Brian,
Thanks for the great tutorial, was just looking into some solutions for adding code snippets to a site, but using a CPT to separate them out def. makes so much sense!
Quick question though, why did you decide to hard-code your archive page vs. using the loop?
I wanted the list to appear with text above it, which really isn’t possible using the standard Genesis loop.
Brian,
This is great. I just downloaded the Types plugin to set up custom post types for a protected area. This seems to be a little (lot) less bulky on the site. You just saved me from having an extra plugin running.
I’m sure the Types plugin is much easier for new users who are unfamiliar (or uncomfortable) with code. But if you feel good about looking things up on the Codex and working in a functions file, yes – this is the way to go.
Hi Brian! I am really excited about creating custom post types. I used the code above to register a custom post type, and it appears in my dashboard.
However, when I create a post, it does publish it. But when I click to view the post, I am taken to a 404 page. Any clue as to what I might be missing?
Thanks!
I just found the answer in the Studio Press Forums. I had to go back and save my Permalink Settings.
I’m thrilled to have it working! Thanks!
Eh, good point – something that I should add tho this. Thanks Gwen, for pointing that out.
Hi Brian,
By the way, thank you for this tutorial but I’m just wondering how can we enable the Genesis Layout Settings Metabox? I attempt to do a value ‘genesis-layout’ but it doesn’t work.
Thanks!
Emman
Thanks for this Brian!
I love this idea of hard coding all of this instead of using a plugin!
However, I followed your steps to the T and cannot seem to get it to work. I can get the custom post type to register, I can create/edit new posts, I can publish new posts, BUT I can’t actually view the posts. It keeps sending me back to the standard Genesis Archives page. Any thoughts?
Thanks again!
Ha ha nevermind… I just read the comment above, re-saved my permalinks and BOOM!
Works great!
Hi Brian.
For client sites, would you ever advise putting this into a plugin?
Thanks.
It really depends on whether or not you trust them. Inside a functions file should be pretty safe.