Join over 55,891 Subscribers Today! FREE UPDATES!
Get The Only Freelancer crash course you will ever need to read!
WordPress is the most popular and the best (in my opinion) blogging platform, created by Automattic. It’s really easy to use and it’s very powerful, giving you the opportunity to build any kind of site, from blogs to shopping carts and CMS’s. In this series of tutorials, we are going to create our first WordPress theme. For this tutorial you need to have a little more than basic knowledge about HTML and CSS. Let’s get started.You can Download source files and you can also Live preview the theme here. It’s a basic css layout so it will be easy for beginners to convert it into a working WordPress theme.
WAMP/XAMPP Server (or similar software) - Download
WordPress 2.9.2 (latest version, recommended) – Download
First thing first, you need to install WAMP Server. After installing WAMP, download and copy the WordPress files in the www directory from your WAMP folder. Start WAMP Server, click on the icon in the task bar, and a pop-up list show appear, then select phpMyAdmin.
Now, create a new database. Give it any name, but you need to keep it in mind as we need it at the WP installation. As for now, we are done with WAMP Server, just leave it running.
After copying all the files, you need to install WordPress. Enter on the address where you have put the WP files. The address would be exactly, without any www or http, localhost or localhost/WordPress. After accessing the address, the installation may start.
Click “Create a Configuration File”. After this, you will see some listing of things you need to know. I’ll just tell you what you need:
Now, just fill in the fields with the required information, and go further! After filling up the fields, next give any name to your blog, and write your e-mail address.
Now, copy the auto generated password, and paste it in the password field at the site login. Now, because you cannot keep that weird password in mind, go the upper right corner of the site and click on “admin”. You should see a page where you have your account info. You can modify anything there, but for now we are setting a new and easier password.
Go to the bottom where it says “New password” and set your new password.
Good, now you have WP installed, but we have a lot more things to do.
When I’m starting a new project on developing a WordPress theme, as everyone should do, I first build my HTML/CSS template. This is easier because you also learn how to convert a simple HTML template into a fully functional WordPress theme. So i won’t go through building the template, considering you already know how to build your own. I’ll just use a simple layout from Free-CSS.com in this tutorial. Go here and download the #7 template.
When I’m making a theme, I’m always copying code snippets from a default WordPress theme (because i cannot remember all of them). I use the WordPress Classic theme, but you can also use a cheat sheet. The best one i’ve found is the one created by Ekinertac. After it opens in the PDF format, save it in your computer (you may need it when you do not have access to the internet).
All the contents from the sheet are the most used functions from WordPress, and the ones that you need to create a basic WP theme.
A thing that you have to notice is that WordPress is using PHP. Even if you aren’t a PHP master, or you have absolutely no idea what PHP is, you are still able to create a basic theme. When creating a theme, you need to know just tags and what they are doing, so do not worry about PHP.
A very important part of a WordPress theme is the CSS file. In the CSS file are written some lines that WordPress is using to g a theme. First, you need to rename the styles.css to style.css as WordPress is search for style not styles. Copy and paste these lines at the start of your CSS file:
/* Theme Name: Free-CSS template WP version Theme URI: <a href="http://free-css.com/">http://free-css.com</a> Description: A very basic WP theme based on a simple CSS layout from Free-CSS.com Version: 1 Author: >your name< Author URI: >your site url< Tags: free, css, tutorial, simple, basic */
You can change everything that is located after the “:” dots. Save your CSS file as we won’t need it because we are just trying to give to the HTML template functionality.
After you set up your CSS file, we need to go and change some things in the index.html file. First of all, clear all the sample text.
Save you file as index.php. We do not need the sample text anymore as we will introduce our own text from WordPress later.
Now, we need to make some declarations for the WP theme. We need to make the title dynamic and to set the stylesheet path. These lines will help you:
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" /> <title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title> <style type="text/css" media="screen">@import url( <?php bloginfo('stylesheet_url'); ?> );</style>
Now, we will start with the header of the design. For adding your blog’s name with a link to your homepage in the header, we will use this line of code:
<h1><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a></h1>
The first PHP tag is used for retrieving the blog’s homepage link and the second is for the blog’s name (the one you set in the installation).
Now we’re going further for the loop. The loop is a block of code that will repeat for each post from WordPress. We will use the loop to construct the article. I’ll cover more about the loop in the second part, where we will use it for other things too.
Now, all your code is wrapper by some loop specific tags. Here they are:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <!--Code to be repeated here--> <?php endwhile; else: ?> <?php endif; ?>
Next, we will start constructing the article structure. We will need a title, some info about the dat when the article was published and it’s author.
The following line makes the article title:
<p><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong></p>
The first PHP tag is for the article’s link and the second is for the article title. These tags are kinda self-explanatory. We’re moving further for the meta data (date, author). Copy and paste the following code under the article title code:
<p>Published on <?php the_time('F d, y'); ?> by <?php the_author(); ?> in <?php the_category(','); ?></p>
First tag is for retrieving the date. The date tag have some parameters. These parameters are setting the date format (month, day, year). You can add characters like “-” or “.” or making something like “Month 02 Day 15 Year 1990″, it’s up to you! The second one, is for echoing the author of the post and the third, is for category or categories that the post is filed under.
Now, we will be placing the content tag. The content tag will output everything you write in a WordPress post:
<?php the_content(); ?>
The tag doesn’t need paragraphs because WordPress auto generates them for you. We’re finished with the loop. You can see the code pieces at the party:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <p><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong></p> <p>Published on <?php the_time('m d Y'); ?> by <?php the_author(); ?></p> <p><?php the_content(); ?></p> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
Next, we will be going to code the sidebar.
We will start with the navigation. As you see in the CSS layout, the navigation is located in the sidebar. It doesn’t matter because the technique is the same. For creating a basic navigation you need these lines of code, which will placed between the <strong>ul </strong>tags:
<li><a href="<?php bloginfo('url'); ?>">Home</a></li> <?php wp_list_pages('title_li='); ?>
You already know what the first PHP tag does. The second one, is listing the pages that WordPress generates. The parameter is for list title. Try removing it and see what’s happening.
This is all you need to do for getting a nice working navigation menu. Now, we will create the categories list. All the blogs have categories and we don’t want our theme to lack categories. We will just add this code between the div #extra tags:
<p><strong>Categories</strong></p> <ul> <?php wp_list_categories('title_li='); ?> </ul>
There is another listing tag but this one is listing the blog’s categories. I’ll cover more things relating the sidebar in part 2, where i will teach you how to make a theme widget-ready. That means you will introduce new blocks inside of WordPress, without coding anything.
But what about the footer? Shouldn’t it have some text in it? Well, we will write the copyright:
<p>Copyright © 2010 <a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a>. All rights reserved.</p>
As you see, we have introduced some tags, just like in the header, to output the blog’s title.
The theme is functional, but we will need to make some page templates for single post (where the comments will appear) and the page (where you could write some info about your blog). If you use the same code for the header footer and sidebar, your template would get messy. Let’s say that you want to add one more thing to your sidebar. You will have to modify 3 times for each page template. Well, for avoiding this, we will chop the markup.
For each section of the index.php file we will have a separate file. So we will have header.php, footer.php and sidebar.php. Now, we will be starting to chop!
Copy and then replace the following lines:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" /> <title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title> <style type="text/css" media="screen"> @import url( <?php bloginfo('stylesheet_url'); ?> ); </style> </head> <body> <div id="container"> <div id="header"><h1><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a> </h1></div>
With the following line:
<?php get_header(); ?>
Your copied code needs to be placed in the header.php file. The same thing we will do with the footer and sidebar
Copy the following:
<div id="navigation"> <p><strong>Navigation Here</strong></p> <ul> <li><a href="<?php bloginfo('url'); ?>">Home</a></li> <?php wp_list_pages('title_li='); ?> </ul> </div> <div id="extra"> <p><strong>Categories</strong></p> <ul> <?php wp_list_categories('title_li='); ?> </ul> </div>
Paste them to sidebar.php then in the index.php file replace it with:
<?php get_sidebar(); ?>
Now, do the same thing with the footer. Copy the footer div and it’s contents, paste it in footer.php file, and then remove it in index.php with:
<?php get_footer(); ?>
You should have something like this:
<?php get_header(); ?> <div id="wrapper"> <div id="content"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <p><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong></p> <p>Published on <?php the_time('m d Y'); ?> by <?php the_author(); ?></p> <p><?php the_content(); ?></p> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?> </div> </div> <?php get_sidebar(); ?> <?php get_footer(); ?> </div> </body> </html>
Now you’re done with chopping the markup. We are going to create the page templates. We will create firstly the single template. Just save your index.php file as single.php. We won’t modify anything in this page yet. We will do it in part 2 where I will learn you how to create a comment form and how to list comments and other things like custom fields.
Now, for the static page, page template, delete the line where you have placed the meta data (the line with date, author and category tags). Then save the file as page.php. Being a page, we won’t need any info about the creation date or the author. We won’t even use comments on page because there is no point in it.
The last move we need to do is to print screen our template and save it in a canvas of 300 x 225 px and save it as screenshot.png in the root directory of your theme. This will be used in WordPress theme menu.
Well, we’re done with the first part. Go ahead and test your theme. If there is something you want to ask, do it via comments. Don’t forget that you can download the source files too.
There are a lot of things to cover about WordPress. Making a theme isn’t so difficult, you just need to be patient. There are a lot of things that are needed to make a fully functional WordPress theme. That’s why, this tutorial will have a Part 2. Maybe there will be more parts, but i will let you know what I will be covering in the next part:
Get The Only Freelancer crash course you will ever need to read!
If not, then it's time to learn how to:
You can trust 1stWebDesigner to help you become a better web designer!
- Jacob Cass | Just Creative
Just enter your name and email below and click Get Updates!