WordCamp Richmond: Starting From Scratch – Introduction to Building Custom Themes

presenter: Wren Lanier

Why use WordPress as a CMS for a small website? It’s flexible enough to build all sorts of kinds of sites. It’s free as in beer and there is a huge support community. It has a beautiful admin (particularly compared to other CMS like Drupal) that clients like to use, which means it is more likely to succeed and make them happy repeat clients.

First things first. Set up a local development server (MAMP or XAMPP) or use a web host. This allows you to develop on a desktop machine as if it were a web server.

Next, download dummy content like posts and comments. There are plugins (WP Dummy Content, Demo Data Creator) or imports in XML form.

Start with a blank theme. You could start from scratch, but nobody needs to reinvent the wheel. Really good ones: Starkers (semantic, thorough, and functional), Naked (created for adding your own XHTML), Blank (now with HTML5), and more.

A blank theme will come with several php files for pages/components and a css file. To create a theme, you really only need an index.php, screenshot.png, and style.css files. Lanier begs you to name your theme (i.e. sign your work).

Now that you have a theme name, start with the header and navigation. Next, take advantage of WPs dynamic tags. Don’t use an absolute path to your style sheet, home page, or anywhere else on your site if possible.

Make things even more awesome with some if/then statements. You can do that in PHP. [I should probably dig out my PHP for Dummies reference type books and read up on this.] This allows you to code elements different depending on what type of page you use.

Once you have your header file, build your footer file, making sure to close any tags you have in your header. Code the copyright year to be dynamic.

It doesn’t have to be a blog!

If you’re going to create a static homepage, make sure you name the custom template. If you don’t do this, the WP admin can’t see it. Go into Reading Settings to select the page you created using the homepage template.

Now that you have all that, what goes into the custom template? Well, you have the header and footer already, so now you put THE LOOP in between a div wrapper. The loop is where WP magic happens. It will display the content depending on the template of the page type. It will limit the number of posts shown on a page, include/exclude categories, list posts by author/category/tag, offset posts, order posts, etc.

Once you have your home page, you’ll want to build the interior pages. There are several strategies. You could let page.php power them, but if you have different interior page designs, then you’ll want to create custom page templates for each. But, that can become inefficient, so Lanier recommends using if/then statements for things like custom sidebars. A technique of awesomeness is using dynamic body IDs, which allows you to target content to specific pages using the body_class tag depending on any number of variables. Or, once again you can use an if/then statement. Other options for body classes.

Finish off your theme with the power of plugins. Basics: Akismet, All-In-One SEO, Google XML Sitemaps, Fast Secure Contact Form (or other contact form plugin), WPtouch iPhone theme. For blogs, you’ll want plugins like Author Highlight, Comment Timeout, SEO Slugs (shortens the URL to SEO-friendly), Thank Me Later (first-timer comments will get an email thanking them and links to other content), and WordPress Related Posts. For a CMS, these are good: Custom Excerpts, Search Permalink, Search Unleashed (or Better Search, since the default search is  bit lacking), WP-PageNavi (instead of older/newer it creates page numbering), and WP Super Cache (caches content pages as static HTML and reduces server load).

Questions:

What about multi-user installations? She used Daren Hoyt’s Mimbo theme because it was primarily a magazine site.

At what point do you have too many conditional statements in a template? It’s a balancing act between which is more efficient: conditional statements or lots of PHP files.

How do you keep track of new plugins and the reliability of programmers? Daren Hoyt & Elliot J. Stock are two designers she follows and will check out their recommendations.

What is your opinions of premium themes? For most people, that’s all they need. She would rather spend her time developing niche things that can’t be handled by standard themes.

How do you know when plugins don’t mesh well with each other? Hard to keep up with this as patches are released and updates to WP code.

Where can you find out how to do what you want to do? The codex can be confusing. It’s often easier to find a theme that does the element you are wanting to do, and then figure out how they designed the loop to handle it.

Are parent templates still necessary? Lanier hasn’t really used them.

Leave WP auto-P on or off? She turns them off. Essentially, WP automatically wraps paragraphs with a p tag, which can mess with your theme.

Leave a Reply

css.php