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:

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.

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:

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.

This entry was posted
on Tuesday, March 8th, 2011 at 3:04 am
and is filed under WordPress.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

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 ?

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.

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.

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.

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.