Using Custom Post Types As CMS Editable Slots in WordPress

Using Custom Post Types As CMS Editable Slots in WordPress

Using WordPress as a fully functional CMS

UPDATE 6/1/11: Added one line to the end of the second chunk of code (wp_reset_query). If you do not have this and place that block above the loop, the id does not get reset and will display the wrong content. Worked if after the loop, but probably a bad idea no to include it.

NOTE: Added one line of code to exlude this content block from search (‘exclude_from_search’ => true,).

There are plenty of ways to use WordPress as a CMS (content management system). There are different plugins that allow you to do it, like MultiEdit, but recently I have been working on building the new corporate site for work, and saw something that my manager did to make WordPress into a very powerful CMS. Normally I build sites in WordPress so that the client can make changes to the main content and widget areas themsevles. That doesn’t require any additional code or tinkering. But what if you need additional areas that the client has to be able to edit themselves as well?

In come “custom post types” to the rescue. I will show you how to easily do this to your own WordPress site, and it only takes a few minutes. I don’t know where my manager got the code for it, but I was impressed with how easy it is.

The functions page

You will need to open the functions page (functions.php) and add this code:

//This is the custom content block
function post_type_customContent() {
     register_post_type(
                     'customContent',
                     array('label' => __('Custom Content'),
                             'public' => true,
                             'show_ui' => true,
                             'exclude_from_search' => true,
                             'supports' => array(
                                                  'title',
                                                  'editor',
                                        'post-thumbnails',
                                        'custom-fields',
                                        'revisions')
                                )
                      );
     register_taxonomy_for_object_type('post_tag', 'customContent');
}
add_action('init', 'post_type_customContent');

Anywhere you see the word “customContent”, you may replace with whatever you want to call it. This just made sense to me, because that is exactly what I am doing. Where you see the words “Custom Content”, can be replaced with what you want the added section in the admin panel to be called. Once you have added that code and uploaded the file, you should see the additional section like the image below.

WordPress Dashboard showing custom content block added

How do you get it to display?

First, you will see in the image, that under “Custom Content”, you have the option to “Add New.” By clicking new, you are adding a new post like you would for any regular post. But you need to be able to get it to display where you want it. Let’s say you have a small section in your header, that has text the client wants to be able to change. In the div, or whatever tag you want this to appear, you place this code:

<?php query_posts('post_type=customContent&page_id=22'); ?>
                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <?php the_content(); ?>
                <?php endwhile; endif; ?>
                <?php wp_reset_query(); ?>

Take special note of the “page_id=22″ because the number is what you have to change. After you have created the new content, publish it and get the id number by hitting the “Preview” button. You will see the number in the url that opens up. Put that number, where the ’22′ is in my example. Now anything that put in that new custom post will display in the desired spot.

Final Thoughts

This opens up WordPress to a whole new level. You can add as many different posts and place the second chunk of code wherever you want it (just changing the post id number when you do). WordPress goes from a blogging system with a small level of editable spots for main content or widgets, to one that is complete. I have absolutely no reason now to not use WordPress on any project.

Coupon Code: webmachine

Tags:

7 Responses to “Using Custom Post Types As CMS Editable Slots in WordPress”

  1. 31k0 says:

    Hey !
    Thanks for the interesting post. I tried to store members of a society with custom types but I failed.
    I didn’t find how to store First and Last Names without the ‘title’ field. Therefore I used a custom MySQL table.
    Do you think that storing data without ‘title’ field is possible with WordPress Custom types ?

    • jcDesigns says:

      Thanks for the comment! I’m not sure I understand the question. You tried storing members with custom post types? I’m not exactly sure why you would do that. How were you gathering the data? Please explain more of what you were trying to do and hopefully I or someone else can help you with your question.

  2. Incredible tips, very useful to place AND edit blocks of content visually by the client.

  3. Sean says:

    I know I’m about a year late to this party, but I’m new to WordPress and have one quick question: When I use a custom post type, does it need to go in the Loop, or can it reside outside of it?

    Thanks!
    -Sean

    • jcDesigns says:

      This is kind of different than the tradition custom post type. This is using them to make additional spots on your website editable and displaying them in a specific area. More of a one off, so I do not think you would want them in the loop in this instance.

      Here, I am just using it to make a section available in the editor so the user can make changes to it like other CMS’s do.

      • Sean says:

        Awesome, that’s exactly what I need to do! Thank you for the reply.

        One last question: When I put the code into functions.php, it causes a server error. I can plug it into posts.php, and it shows up in the admin dashboard, but when I try to get the content (images that load into a slideshow) to display using the ‘query_posts’ technique you’ve shown, the images shows up where another post (the main page text) should.

        Do you have any advice, or can you point out where I made a mistake?

      • jcDesigns says:

        Hey Sean, thanks for the comments. Only one thing pops into my head about the content showing up wrong, which sounds like on the line that says: customContent&page_id=22 is that you have the wrong id number there. The error with it being in the functions is odd. I have done this twice now using my article (cause I can never remember how to do this) and it has come out fine. If you want to email me the functions page and the page you are posting the query part, I can try to help you out. You can email me at contact[at]jc-designs.net. Replace [at] with @ in that address.

Leave a Reply