• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Toggler
      • on February 4, 2010
    • Book review - jQuery 1.3 with PHP
      • on January 6, 2010
    • 2009 in a few words
      • on January 2, 2010
    • Bad Firebug!
      • on December 21, 2009
    • Posterous
      • on December 2, 2009
    • Chaining routes in Zend Framework
      • on November 27, 2009
    • Zend Framework bug hunt days
      • on November 22, 2009
    • Zend Framework 1.8 Web Application Development book review
      • on November 17, 2009
    • A book review
      • on October 11, 2009
    • Playing with Zend_Navigation and routes
      • on August 9, 2009
  • Recent Comments

    • Aryashree Pritikrishna
      • on January 28th @ 9:10 am
    • Michl
      • on January 15th @ 10:09 am
    • Robert
      • on January 2nd @ 1:36 pm
    • Ivan
      • on January 2nd @ 1:33 pm
    • Keith Pope
      • on January 1st @ 11:57 am
    • Jani Hartikainen
      • on December 29th @ 8:55 am
    • johnjbarton
      • on December 22nd @ 1:01 am
    • Robert
      • on December 21st @ 11:55 pm
    • René Silva
      • on December 21st @ 11:47 pm
    • Robert van Drunen
      • on December 21st @ 6:37 pm
  • Tags

    • php
    • framework
    • zend
    • example
    • random
    • about
    • site
    • ubuntu
    • blog
    • introduction
    • book
    • wordpress
    • linux
    • apache
    • lamp
    • setup
    • review
    • open source
    • svn
    • comic
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Archives

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • August 2009
    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework

Wordpress as CMS tutorial

by Robert Basic on March 14th, 2009

Wordpress is one of the best blogging platforms out there — if not the best. It’s very powerful, can be easily extended and modified. It’s documentation is very well written and, so far, had answer to all of my crazy questions :)

You know what’s the best part of Wordpress? With some knowledge of PHP and MySql, you can turn it into much more than just a blogging platform. After doing some HTML to WP work for Roger, I thought of one way how could Wordpress be transformed into a CMS. Note the “one way”. This is not the only way for doing this, and, most likely, not the best way.

I didn’t look much, but I think that there are some nice plugins out there that can do this. But, where’s the fun in the download, upload, activate process? Nowhere!

I will show you how to change your Wordpress into a CMS and it really doesn’t take much coding to achieve this! The example presented here is simple and will have a static page for it’s home page, another static page for the “Portfolio” page and the blog. The home and portfolio page will have some of own content and both will include some content from other static pages. You all most likely know the blog part ;)

Static pages

Things you should know: each static page has it’s title, it’s slug or name (the thing that shows up in your browsers address bar: http://example.com/portfolio/ - right there, the portfolio is the slug!), has the parent attribute and the template attribute. The parent attribute is used when it’s needed to make one page a child of another, i.e. to show Page2 as a subpage of Page1. The template attribute is used when we want to apply some different layout and styling to a static page. Read more about static pages and how to create your own page templates.

If you want to, you can download the theme I created for this tutorial from here (it’s not a designers masterpiece, what did you expect?), or you can use any theme you want.

I hope you read the part on creating page templates, I really don’t feel like explaining the next part.

Create 3 new files in your template directory (if you’re using my theme, these files are already there): home.php, portfolio.php and blog.php. Contents of these files are:

// home.php
<?php
/*
Template Name: Home
*/
?>

// portfolio.php
<?php
/*
Template Name: Portfolio
*/
?>

// blog.php
<?php
/*
Template Name: Blog
*/

// Which page of the blog are we on?
$paged = get_query_var('paged');
query_posts('cat=-0&paged='.$paged);

// make posts print only the first part with a link to rest of the post.
global $more;
$more = 0;

//load index to show blog
load_template(TEMPLATEPATH . '/index.php');
?>

To understand the contents of the blog.php file, please take a look at this.

Now, go to the dashboard, the Pages section and write 3 new static pages:

  • Home, with the slug home, for the template choose Home from the drop-down list (it’s on the right side) and the parent leave as is (Main Page)
  • Portfolio, with the slug portfolio, for the template choose Portfolio
  • Blog, with the slug blog, for the template choose Blog

You can add some content to the Home and Portfolio pages, but don’t add any to the Blog page.

Organizing links

Now, let’s make that when we are on http://example.com/ it shows us the Home page, instead of the Blog, and when on the http://example.com/blog/ to show us the blog!

Go to Settings->Reading and where it says “First page displays” choose “A static page”, and under the “Front page” drop-down choose “Home”.

Now, go to Settings->Permalinks and change the “Custom structure” to /blog/%postname%/ or whatever is your preferred permalinks structure, but it must start with /blog/! If Wordpress can’t write to your .htaccess file (I hope it can’t!), open it up in your editor and type the following (or similar, depends on your setup):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

The point is in the RewriteBase, with that we’re telling WP where to find the blog. On default setups, when http://example.com/ points to the blog, the RewriteBase is simply / but with the blog located at http://example.com/blog/ we need to change the RewriteBase. If all is well, we’re done with organizing the links.

While you’re still in the dashboard, write some new static pages with content. For the parent of these pages choose Portfolio and leave the template the default (the default page template is page.php).

Time for coding!

Here are two functions I wrote for retrieving content from static pages which will be then included in other static pages:

// functions.php
<?php

/**
* Gets last $number_of_subpages from their $parent_page
* If the <!--more--> tag is ignored ($ignore_more=true) returns the entire content of the subpages
*
* @param mixed $parent_page Contains either the slug of the parent page or it's ID
* @param integer $number_of_subpages Number of subpages to return
* @param boolean $ignore_more Whether to ignore the <!--more--> tag or not
* @return array Contents and titles of subapages
*/
function wpascms_get_subpages($parent_page='portfolio', $number_of_subpages=2, $ignore_more=false)
{
    global $wpdb;

    if(is_string($parent_page))
    {
        $parent_page_ID = wpascms_get_parent_page_ID($parent_page);
    }
    else
    {
        $parent_page_ID = $parent_page;
    }  

    if($number_of_subpages == 0)
    {
        $limit = '';
    }
    else
    {
        $limit = 'LIMIT 0, ' . $number_of_subpages;
    }
    // Get all subpages that are published and are childs of the given parent page
    // and order them by date in descending order (latest first)
    // also, if needed, limit to the latest $number_of_subpages
    $subpages = $wpdb->get_results("SELECT * FROM $wpdb->posts
                                    WHERE `post_parent` = '$parent_page_ID' AND `post_type` = 'page' AND `post_status` = 'publish'
                                    ORDER BY `post_date` DESC $limit");

    if(!$ignore_more)
    {
        foreach($subpages as $key=>$subpage)
            if(strpos($subpage->post_content, '<!--more-->') !== false)
            {
                $short_content = explode('<!--more-->', $subpage->post_content, 2);
                $subpages[$key]->post_content = $short_content[0] . '<a href="' . get_permalink($subpage->ID) . '">Read more...</a>';
            }
        }
    }

    return $subpages;
}

function wpascms_get_parent_page_ID($parent_page)
{
    global $wpdb;

    $id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE `post_name` = %s AND `post_type` = 'page' AND `post_status` = 'publish'", $parent_page));

    return $id;
}

?>

The first function, wpascms_get_subpages() returns the given number of subpages from a specific parent page. By default it will break the content on the <!–more–> tag and append a “Read more…” link. The first parameter can be either a string containing the slug of the parent page, or the ID of the parent page. The second parameter is the number of subpages we want returned. If it’s zero, all subpages will be returned. The second function is merely a helper function, to get the id of the parent page based on it’s slug. To read more on querying the database, read this page.

Here’s how I’m calling this function on my example Home page:

<?php
/*
Template name: Home
*/

get_header();
?>

    <div id="home">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <h2><?php the_title(); ?></h2>

        <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>

    <?php endwhile; endif; ?>
    </div><!-- home -->

    <div id="latest_works">
    <h1>Recent work</h1>
    <?php $subpages = wpascms_get_subpages();
    if(count($subpages) > 0):
        foreach($subpages as $row=>$subpage):
        if($row%2 == 0)
        {
            $class = "left_work";
        }
        else
        {
            $class = "right_work";
        }
    ?>
        <div class="<?php echo $class; ?>">
            <h2><a href=<?php echo get_permalink($subpage->ID); ?>><?php echo $subpage->post_title; ?></a></h2>
                <?php echo $subpage->post_content; ?>
        </div>
    <?php
        endforeach;
    endif;
    ?>
    </div><!-- latest_works -->

<?php
get_footer();
?>

In words: including the header, then showing any content of the home page. After that getting the subpages: by default, wpascms_get_subpages() is getting the newest 2 subpages of the portfolio page. I’m showing the content of the subpages in 2 columns. What we got with this? Add a new subpage to the portfolio and it will automagically show up on the left side column. In the end, including the footer.

Here’s another example from the portfolio page:

<?php
/*
Template name: Portfolio
*/

get_header();
?>

    <div id="portfolio">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <h2><?php the_title(); ?></h2>

        <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>

    <?php endwhile; endif; ?>
    </div><!-- home -->

    <div id="latest_works">

    <?php $subpages = wpascms_get_subpages('portfolio', 0);
    if(count($subpages) > 0):
        foreach($subpages as $row=>$subpage):
    ?>
        <div class="work">
            <h2><a href=<?php echo get_permalink($subpage->ID); ?>><?php echo $subpage->post_title; ?></a></h2>
                <?php echo $subpage->post_content; ?>
        </div>
    <?php
        endforeach;
    endif;
    ?>
    </div><!-- latest_works -->

<?php
get_footer();
?>

Same thing is happening here: including the header, showing the content of the portfolio page. Getting the subpages, but now all of the subpages that are childs of the portfolio page, and showing them one under the other.

All subpages can be viewed each on it’s own page, but that is just a plain ol’ page.php file, so I’ll skip that.

And now something completely different

I made a screencast to show this in action. Please forgive me on my bad accent. This will just show how much I suck in speaking English. Oh, well.


Wordpress as CMS from Robert Basic on Vimeo.

Don’t limit yourself to the existing plugins or waiting for one tutorial/example that will show how you can make everything. Don’t be afraid to get your hands dirty by hacking some code. It really doesn’t take too much to create magic with Wordpress ;)

Cheers!

Share this post:
  • Digg
  • description
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Reddit
  • TwitThis
  • Google
  • E-mail this story to a friend!
Other posts you might be interested in: TickTweet WordPress plug-in, Wordpress paging navigation, Starting with Zend Framework - part 2, A Zend_Captcha example, Bad Firebug!, or just wonder on through the archives...
If you liked this post, you can buy me a cup of coffee!
Tags: blog, cms, example, hack, php, tutorial, wordpress.
Categories: Development, Programming, Software.
Subscribe to the feed.

Comments: 27

Grab the comments feed

  • links for 2009-03-15 » Von admin » Beitrag » von pixeln /&/ paddeln

  • March 16th, 2009

[...] Wordpress as CMS tutorial ~ Robert Basic (tags: wordpress tutorial cms) [...]

  • Antonio Thonis

  • March 16th, 2009

Thank you very nice tutorial. Is it possible you release this theme? The layout is almost exactly what i want my page to look like, but that with a micro blog under the projects.

  • Robert

  • March 16th, 2009

Hi Antonio! Glad you like it :) The “theme” I used is here: http://robertbasic.com/downloads/wpascms.zip it’s unfinished, cause I made only those parts I need for this tutorial. Feel free to use it, change it, rename it, whatever :)

Cheers!

  • Jon Lebensold

  • March 16th, 2009

Hey Robert, quality looks good. Well done!

  • Robert

  • March 16th, 2009

Thanks Jon, especially for the tips! I managed to get the size to 5MB which is quite a difference from 19MB :)

  • Robert Basic’s Blog: Wordpress as CMS tutorial | Shoultes.net

  • March 20th, 2009

[...] Basic has written up an entire tutorial on how to use an install of the popular PHP blogging engine, WordPress, as a content management [...]

  • iSmart Design » WordPress como CMS

  • March 23rd, 2009

[...] Hace ya 5 años aproximadamente que tuve mi primer contacto con Word Press, y desde entonces se ha convertido en mi aplicación favorita para blogging, prueba de ello es que este blog esta administrado por este. Actualmente Word Press se ha vuelto bastante dinámico y prueba de ello es que varios sitios están usando esta plataforma open source como CMS, es muy interesante ver como se puede transformar para dar vida a un sitio entero. En Internet ya hay varios tutoriales circulando para poder lograr hacer que WP funcione de esta manera, por el momento les dejo un tutorial que a mi parecer es ampliamente explicito para lograr aplicar WP como un CMS. [...]

  • Daily Digest for 2009-03-25 | Pedro Trindade

  • March 26th, 2009

[...] Wordpress as CMS tutorial ~ Robert Basic [...]

  • kaotoxin

  • March 27th, 2009

Hi there!
Thank you very very much for that tutorial!
I’ve implemented the code in the site I’m currently developping and with some tweaking it works perfectly fine!

  • Enk.

  • March 27th, 2009

Hi,
Its a cool written artcile.
I converted my wordpress blog to website by just a couple of tweaks like creating custome page templates + custome sidebars and writing static pages. Then A great plugin ex-clude pages, this plugin excludes your selected pages from the navigation bar. So its now working like a cool website.. :P

  • John

  • April 1st, 2009

Great tutorial, I really enjoyed reading about that. I have been thinking about setting up a website and figuring out some easy ways to do that. Wordpress has been my first choice because it is easy to set up and easily customizable. What you did to customize the website in the tutorial is almost exactly what I want to do with my website. I will take your advice and see how it goes. Thanks so much for the great blog content, I will be coming back to check for more great stuff. And your English is not that bad!

  • Tutorial Extjs: Edit, Delete and Add File to Database | Defafe

  • April 3rd, 2009

[...] Wordpress as CMS tutorial (robertbasic.com) [...]

  • 5 articles you must read about Wordpress | Lacisoft's

  • April 7th, 2009

[...] 1) Wordpress as CMS tutorial - http://robertbasic.com/blog/wordpress-as-cms-tutorial/ [...]

  • joe

  • April 9th, 2009

Nice code I’ll be trying that out on my new blog. Yeah I am amazed at the flexibility of Wordpress. I has plugins and now after reading your tutorial I realize it can help me template my site too!

  • Gene

  • April 12th, 2009

Thanks for this great tutorial.. I have been looking for wordpress tutorials which I can truly understand.. And I guess I found it.. I’ll be making use of this for my personal blog.. Great work!

  • 21 Resourcen zu “Wordpress als CMS” | Bugeyes.de

  • April 20th, 2009

[...] Wordpress as CMS tutorial - (Artikel von robertbasic.com) [...]

  • Dougal Matthews

  • April 20th, 2009

I’ve taken this appraoch with Wordpress before - it is really quite cool for doing fairly small CMS projects and such. However, I found once the website grows it starts to get quite messy.

This website; http://www.braemargolf.co.uk/ for example is written with Wordpress used as a CMS. It’s got a fully custom theme and I done various modifications to the wp-admin to remove and add things.

  • Nadeesha

  • April 20th, 2009

Yeah, Wordpress definitely makes things a whole lot easier! Great Tutorial too.. Excellent Coding Robert, love the design too ! :)

  • Wordpress como CMS en páginas que no son blogs. | Mareos de un geek

  • April 29th, 2009

[...] Convertir Wordpress en un CMS. [...]

  • William Catling

  • May 5th, 2009

I was looking for info on exactly this haha perfect! I mean I knew it could be done just with how wordpress is setup, it’s just getting the “static” look that gives it a non “blog” feel. After that just seems like setting parent categories and their children and so on…

Bookmarked and I will be back when I start on my computer business site, which I wanna do with wordpress as the CMS but I need to read over what you wrote a few more times.

Do you know of a good forum where I can even ask questions or read other peoples questions about wordpress as a CMS or more advanced things with wordpress? Thanks much Robert!

Will

  • marc andrew

  • May 9th, 2009

I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work. Look forward to reading more from you in the future.

  • Leonel Richards

  • May 13th, 2009

Nice design you got here Robert, I’ve had issues in creating my own templates lately,the information came in handy to me. A lot of tips that can help me improve actually. Thanks so much for the post!

  • Frank

  • May 14th, 2009

Hi,

Great tutorial. I’m going to try this out as my website currently runs on Joomla after having a wordpress blog for 3 years. But Joomla doesn’t quite cut it for me. So i’ll have another go with wordpress. And i bet it’s gonna work after reading your article.

Thanks for this.

Frank

  • Darin R.

  • May 20th, 2009

Great tutorial mate! Keep up the good work.

  • Hellas

  • May 27th, 2009

Great tutorial and info. I will try few of the tips on my next site.

I am picking really good stuff from your blog :D thanks

  • Wordpress as CMS tutorial ~ Robert Basic

  • July 3rd, 2009

[...] the original post here: Wordpress as CMS tutorial ~ Robert Basic Tags: cms Comments0 Leave a Reply Click here to cancel [...]

  • Miguel Palazzo

  • November 18th, 2009

Hah! Thank you again mate! I got to this post and found exactly what I needed to do with my blog.php template :D

Leave a Reply

 

Robert Basic © 2008 — 2010
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on November 30th, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS