Book Image

WordPress 2.8 Themes Cookbook

By : Nick Ohrn, Lee Jordan
Book Image

WordPress 2.8 Themes Cookbook

By: Nick Ohrn, Lee Jordan

Overview of this book

Themes are among the most powerful features that can be used to customize a web site and give it a professional look, especially in WordPress. Using custom themes you can brand your site for a particular corporate image, ensure standards compliance, and create easily navigable layouts. But most WordPress users still continue to use default themes as developing and deploying themes that are flexible and easily maintainable is not always straightforward and lot of issues pop up during the process.This easy-to-use step-by-step guide will help you create powerful themes for your WordPress web site, and solve your theme development problems in a quick and effective way. It enables you to take full control over your site's design and branding and make it look smarter.WordPress is distributed with two ready-to-use themes. You can use these themes to give a common look to your website, or use the techniques described in this book to create custom themes. This book includes over 100 useful recipes to help you get started and create advanced themes. It starts with the basics of WordPress themes and creating a theme from scratch. Then, it covers how to enhance your template and add effects to get a rich look. You will learn how to manage pages, categories, and tags for your blogs, and how to make your posts look unique. You will also learn about the comment system and sidebars that will help you give a new feel to your blog and web site.This book will help you through the most common problems encountered when developing a WordPress theme. You will get tips to enhance your design skill and eventually enhance your blog's design.
Table of Contents (17 chapters)
WordPress 2.8 Themes Cookbook
Credits
About the Authors
About the Reviewers
Preface

Creating a theme from scratch


Creating a great theme from scratch is a challenging task. You have to define markup and behaviour, and add all of the necessary styles yourself. That being said, building from the ground up is sometimes the only thing that makes sense if you're building something really special.

Although making sure everything works correctly when you're finished will be difficult, getting started with your theme is not. There are only a few files required to get you going. After that, though, you'll be on your own as far as making sure that all of the appropriate information gets displayed.

How to do it...

First, create a new directory to contain your theme, and name it whatever you want. If you need help figuring out where to place your theme, see the recipe Installing and activating a theme.

Next, create the following files inside your newly-created directory:

  • style.css

  • index.php

The theme's main stylesheet (style.css) is required to contain information about the theme in a particular format. This is very important. Without this information, WordPress will not be able to correctly recognize your theme. Open style.css and insert the following:

/*
Theme Name: Your Theme Name
Theme URI: http://example.com
Description: Write a short description.
Author: Your Name
Author URI: http://example.com
*/

After inserting the base structure, you are free to change it to whatever you see fit. For my purposes, I've changed the code to read as follows:

/*
Theme Name: WordPress Themes Cookbook
Theme URI: http://plugin-developer.com/wordpress-themes-cookbook-theme/
Description: A demonstration theme for the WordPress Themes Cookbook.
Author: Nick Ohrn
Author URI: http://plugin-developer.com
*/

Now, to test that you correctly entered all the information, you need to visit the Manage Themes section of the WordPress administration panel. Open up the WordPress administration interface and click on Appearance. Scroll down, and you should see a box that contains all of the information for your newly-created theme. Given the information that I entered, my box looks like the following:

Your Manage Themes page should display the information that you entered. For more information on the different items that your style.css file can contain, see the official WordPress documentation at http://codex.wordpress.org/Theme_Development#Theme_Style_Sheet.

There's more...

WordPress themes generally contain a variety of different files to display data of different types and organizations. In addition to the required style.css and index.php files, you can create specially-named files that will handle certain situations.

Recognized WordPress files

WordPress recognizes and uses a variety of files for different situations. A full list of files and their use can be found at http://codex.wordpress.org/Theme_Development#Theme_Template_Files_List. The following list describes the most common files and the purposes for which they are used:

  • home.php—used to display the home page

  • single.php—used to display a single post

  • page.php—used to display a single page

  • category.php—used to display a category archive

  • author.php—used to display an author archive

  • date.php—used to display a date- or time-based archive

  • archive.php—used to display a generic archive if category.php, author.php, or date.php are not present

  • search.php—used to display search results

  • 404.php—used when no results match a query

Organizing a theme

WordPress recognizes that a good theme will be well-organized and often has a consistent header, sidebar, footer, and comments section. As such, the following files are supported for separating those elements out, and are included with special WordPress functions:

  • header.php—get_header()

  • footer.php—get_footer()

  • sidebar.php—get_sidebar()

  • comments.php—comment_form()

For more information on these functions, see http://codex.wordpress.org/Theme_Development#Basic_Templates.

See also

  • Installing and activating a theme