1. Code
  2. WordPress

WordPress: Beginner to Master, Part 1

Read Time:12 minsLanguages:

Throughout this six-part beginner-to-master series, we'll be using the advanced features of WordPress to create our own portfolio & blog, complete with an options page, multiple styles and support for the new WordPress 2.7 features.

In day one, we're handling the preparation and admin sections by creating our Options page.

Also available in this series:

  1. WordPress: Beginner to Master, Part 1
  2. WordPress: Beginner to Master, Part 2
  3. WordPress: Beginner to Master, Part 3
  4. WordPress: Beginner to Master, Part 4
  5. WordPress: Beginner to Master, Part 5
  6. WordPress: Beginner to Master, Part 6


By the end of this series, we will have created Innovation – a premium-quality WordPress template perfect for showcasing a portfolio and hosting a blog; complete with the following features:

  • Options page for handling the advanced nature of the template, without having to edit any files manually.
  • 3 built-in colour schemes – with easy switching between them.
  • Homepage and Portfolio page templates, with a widget-ready footer and sidebar.
  • Compatibility with WordPress 2.5+.
  • Threaded & paged comments support for WordPress 2.7.
  • A custom widget for displaying your latest posts, with a preview image, on your homepage.

The theme will come with three styles built-in, DeepBlue, RedSpace and SoftLight, but you will be able to easily add your own colour schemes:

The plan for each day:

  1. Preparation & Admin Options page.
  2. Homepage layout. Creating a custom WordPress loop. Using widget areas. Footer.
  3. Blog posts loop. Sidebar. Single blog layout.
  4. Portfolio page. Seperating 'single' pages dependant on category. Single portfolio page.
  5. Comments layout. Archives. Search.
  6. Face lift – the 'RedSpace' alternative colour scheme. Creating a custom widget.


What previous experience do I need?

Experience with xHTML & CSS is a must (as I won't go over much of this code in detail).

It would also be very useful to have some basic understanding of PHP (such as using variables, arrays, conditional statements and 'foreach' and 'while' loops. Check out Jeffrey's excellent "Diving into PHP" screencast series.

Having some knowledge of WordPress (even just knowing your way around the Dashboard!) would definitely improve your experience. Check out Drew's "WordPress for Designers" screencast series.

Jump to a Section

Getting Started

Today, we will be handling the core back-end code which the rest of theme will rely upon. You will first need a fresh install of WordPress, preferably the latest version (2.7 at time of writing); however the template will work with any version above 2.5. Inside the /wp-content/themes/ folder, create new folders and files with the following structure:

  • /innovation/
    • /styles/
      • /deepblue/
      • deepblue.css
    • functions.php
    • index.php
    • page.php
    • page-home.php
    • page-portfolio.php
    • style.css
    • var.php

functions.php will contain code for our options page and widgets amongst other things.

index.php is used for displaying latest posts.

page.php will the template used for most single pages.

page-home.php will be our frontpage template.

page-portfolio.php will be our portfolio page.

var.php will be used to retrieve our options from the database.

style.css will contain most of our styling.

/styles/deepblue.css will contain the styling for our first colour-scheme.

These aren't all the files we will need. We'll create the others as we go along so that you can understand what each is for.

Preparing Files

The two page- files are custom page templates, so that we can lay content out differently on pages assigned these templates. For WordPress to recognize them, inside page-home.php enter the following:

Similarly, inside page-portfolio.php enter:

The Template Name is the name used by WordPress when selecting a page template. get_header(); is a WordPress function to load the header.php file (which we'll create later).

And add the following to page.php. We don't need to give this file a template name, as it is the default template.

We also need to assign some details such as name, description and author for our theme. This is done in a comment block at the top of style.css:

Categories & Pages

To save time later, we will create the relevant categories & pages we will need now. In your WordPress dashboard, activate the theme (it will be using the name we defined in the CSS previously) - don't worry that the theme is empty, we're moving to that later.

Now create three new Pages. One named 'Home', and the other 'Portfolio'. Select the Page Template for each of them as 'Homepage' and 'Portfolio' respectively. Name the final page 'Blog' and use the default page template.

Also create a new category named 'Portfolio'. This is what we will use to post portfolio items in.

Make WordPress a CMS

From your Dashboard, go to Settings -> Reading. For 'Front Page Display' set Front page to the 'Home' page you created, and Posts page to 'Blog':

Options Page

This is probably going to seem a little backwards, but we are going to create an options page for our theme before we do any work on the actual front-end. This is because the entire theme depends upon the options on the page, and it wouldn't make sense to do those other parts first.

For those who don't know, the options page is an extra page in the WordPress Dashboard we are adding which holds a number of options for our theme. These are:

  • Colour Scheme
  • Portfolio Category
  • Blog Page
  • Google Analytics code

Inside functions.php start with the following. The $themename variable defines the name of the theme which will be used for the page's title in the Dashboard (Innovation). And $shortname is used to make our options unique - if you change this, you will have to change several instances throughout the code (so don't change it ;)).

Categories Drop-Down

Next we will create the code which gets all the categories for us to use in the 'Portfolio' drop-down option:

This code uses WordPress' get_categories(); function to retrieve a list of all the category names, and using a foreach(); loop to add each name & ID into an array named $getcat for use later in the code. Finally, we use array_unshift to insert "Choose a category:" at the top of the array.

Pages Drop-Down

Next is another similar drop-down option which selects a page name. The code for this is very similar to the previous one:

Like getting the categories, we are using WordPress' get_pages(); function to get a list of all the page names, and the result is placed into the $getpag array for use later.

Stylesheet Drop-Down

There is also a drop-down option for selecting a stylesheet. This code is a little more complicated:

The above code uses the PHP opendir(); function, followed by readdir(); to open our /styles/ folder and get a list of all the files in there.

We then use stristr(); to limit the list to only the .css files (to stop the folders from being outputted aswell), and the results are passed into the $styles array.

The Options Array

All our options will be stored in their own arrays so that we can mass-process them later in the code:

Each array is one option, and contains name, desc, id and type parameters. The ID is very important, and is what we'll be using to reference the option later in the code. For example, to get the Portfolio Category, we would check the $ts_portfolio_cat (ts is the shortname we referenced at the top of the document.)

Options Back-end

The following code makes our options page appear in the Dashboard (it's under "Design" in WordPress 2.6 or below), and saves the options into the database.

We're not going to go through the code as it would take this tutorial off-track too much. Just copy & paste it and think of it as some magic code from the WordPress Gods which makes your theme work.

This next code checks each of our options for their type attribute, and styles them into a table. So options with "type" => "select" would output as a drop-down box, or "type" => "textarea" would output as a textarea.

Finally, at the end of the file, insert:

We require() our var.php file which will contain code for retrieving our options.

Note that we have to use TEMPLATEPATH in require() or include() tags so that it is browsing to our theme directory, and not the root WordPress directory.

Retrieving These Options

Now those options are in the Dashboard, we need to be able to use them in our theme. Insert the following code into var.php:

The first section of the code retrieves all our options from the database. However we have a problem: our Category & Page options send back the name of the category or page - but we'd prefer the ID as it's easier to use in WordPress functions.

The next two parts of the code do exactly that. We run a SQL query to get the appropriate ID. These options are now stored in the $ts_portfolio_cat and $ts_blogpage variables.

Now we have the admin side finished, login to your Dashboard, and set the appropriate settings for each of the options. Choose deepblue.css for the colour-scheme for now.


That concludes Day 1 of WordPress Week. Tomorrow, we'll be creating our homepage layout, making a custom 'WordPress Loop' and handeling widget areas.

Part 2 - Loops and Widgets.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.