How I Built Code Snippets With Custom Post Types

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.

Email Newsletter

Like what you read here in this blog post?
Get more like it delivered to your inbox daily.

Comments

  1. says

    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.

    • says

      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!

  2. says

    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!

    • says

      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.

  3. says

    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.

    • says

      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.

      • says

        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.

  4. says

    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

  5. says

    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!

  6. says

    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.

  7. Carlos says

    Thanks for the great info Brian!

    I have a “how-to” request:

    “How to use the genesis featured posts widget with custom post types”

    • says

      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).

      • Carlos says

        @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.

  8. says

    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.

  9. says

    Thank you for this! :D 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. :)

  10. says

    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 ;-)

  11. says

    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?

  12. says

    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.

    • says

      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.

  13. says

    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!

  14. says

    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

  15. says

    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!

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>