Using Custom Post Types and Custom Fields to Spruce Up WordPress

Why are they useful?

As many people know by now, WordPress is way more than a blogging platform these days. It can be used as a fully functional CMS (Content Management System), allowing unlimited possibilities for content placement, if you know a few little tricks.

Custom Post Types and Custom Fields can change the way you view WordPress as a CMS. As a developer, they can give you a way to display content on a page exactly the way you want, and keep it super-consistent. As an end-user, they give you a way to easily input data, without worrying about knowing HTML/CSS to display that data in a structured way.

In this post, I’ll not only teach you to add Custom Post Types and Custom Fields to your WordPress site, but I’ll also show you how to place the content in a template. This post assumes you know your way around a WordPress install, but I will also include ways to do this without any prior knowledge of PHP or WordPress templating.

Post types come in handy when you have multiple instances of the same type of data. For example, if you want to list many businesses in a directory you could have a Post Type called Businesses and enter each business as a separate post. You can even add tags and categories to your Custom Post Type just like with regular posts, and sort them in the loop the same way as well.

The Nitty Gritty

To add a Custom Post Type, you need to open your functions.php file in your wordpress template. If you don’t have one, make one. Google is your friend if you don’t know what you’re doing. Insert this snippet inside of the tags of the functions.php file. If this seems a bit much for you, check out this Custom Post Type code generator.

add_action( 'init', 'register_cpt_business' );

function register_cpt_business() {

    $labels = array( 
        'name' => _x( 'Businesses', 'business' ),
        'singular_name' => _x( 'Business', 'business' ),
        'add_new' => _x( 'Add New', 'business' ),
        'add_new_item' => _x( 'Add New Business', 'business' ),
        'edit_item' => _x( 'Edit Business', 'business' ),
        'new_item' => _x( 'New Business', 'business' ),
        'view_item' => _x( 'View Business', 'business' ),
        'search_items' => _x( 'Search Businesses', 'business' ),
        'not_found' => _x( 'No businesses found', 'business' ),
        'not_found_in_trash' => _x( 'No businesses found in Trash', 'business' ),
        'parent_item_colon' => _x( 'Parent Business:', 'business' ),
        'menu_name' => _x( 'Businesses', 'business' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'All Businesses',
        'supports' => array( 'title', 'editor', 'custom-fields' ),
        'taxonomies' => array( 'category', 'post_tag' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,

        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'business', $args );
}

Obviously you will want to change the labels to the suit your needs, but you should be able to piece together what goes where.

So your post type should show up in your Admin Sidebar now, but you may notice that your posts aren’t working. This is a little bug with WordPress I guess, but there is an easy fix. Click Settings => Permalinks on your Admin Sidebar, and just go in and hit save changes. No need to change any settings at all. This will just reset your htaccess settings to recognize the new Post Type.

OK, now you have a Custom Post Type ready to go. But let’s say that your businesses, or whatever post type you used, have a lot of information that needs to be displayed. You want to make it really easy for someone to put that information in, and keep it looking nice on the front-end. With Custom Fields, it’s a cinch. You can manually create custom fields , which is a more lightweight method of creating them, but I personally think a plugin called Advanced Custom Fields  works just fine.

It’s super-simple, super-powerful and does exactly what we need it to do. It’s fairly easy to figure out. Create a Field Group, insert fields into the group and tell it which post types that group will be visible. Your fields will now show up when you edit the posts.

Displaying the Data

Now, for the templating. With the Advanced Custom Fields plugin, all you need to do to display the information in the field is add this little tag, and replacing the text inside of the quotes with the ID of the field you created.

I put in the ‘If’ statement to ensure that there isn’t an empty DIV where the address should go if they do not enter one. This is totally optional, and all depends on how you’re using the fields.

If you decided to go with manually adding the custom fields, the tutorial I linked to above explains how to display them.

I hope this blog post has changed your outlook on WordPress as a CMS. Custom Posts and Fields are just one example of the many things you can do with WordPress to make it more powerful and useful as a CMS.

What are some of the ways you customize your WordPress sites? We would love to hear about them!