Creating a WordPress Theme HTML Structure

Now we’re starting to get into the real meat of WordPress Theme development: coding the HTML structure.

The Goals of Any HTML Structure

When coding a web site you should have 2 goals in mind: lean code and meaningful code. That is, using as little markup (HTML tags) as possible and making sure that the markup is meaningful by using semantic class and ID names that refer to their content, not how they “look” (class=”widget-area” instead of class=”sidebar-left”).

Now, when coding a WordPress Theme (or any template for any Content Management System) a balance is going to have to be struck between lean markup, with very little structure, and what’s called Divitis; including too many unnecessary div elements in your code. In other words, too much meaningless structure.

You’ve probably seen the div tag before if you’ve looked at the code for a website or any WordPress Theme. Think of them as containers for HTML code—containers that are very handy for manipulating HTML code with CSS. Obviously we’re going to have some. But we don’t want too many or ones without meaning. And we want enough structure—using the div tag—that we can reuse our code for multiple blog and site layouts. We want to code something we can come back to and use again.

The HTML Structure for Your WordPress Theme

Let’s take a look at the HTML structure we’ll be using for the body of our WordPress Theme.

  1. <html>
  2. <head>
  3. </head>
  4.  
  5. <body>
  6. <div id="wrapper" class="hfeed">
  7.  <div id="header">
  8.   <div id="masthead">
  9.  
  10.    <div id="branding">
  11.    </div><!– #branding –>
  12.    
  13.    <div id="access">
  14.    </div><!– #access –>
  15.    
  16.   </div><!– #masthead –>
  17.  </div><!– #header –>
  18.  
  19.  <div id="main">
  20.   <div id="container">
  21.  
  22.    <div id="content">
  23.    </div><!– #content –>
  24.    
  25.   </div><!– #container –>
  26.  
  27.   <div id="primary" class="widget-area">
  28.   </div><!– #primary .widget-area –>
  29.  
  30.   <div id="secondary" class="widget-area">
  31.   </div><!– #secondary –>
  32.  </div><!– #main –>
  33.  
  34.  <div id="footer">
  35.   <div id="colophon">
  36.  
  37.    <div id="site-info">
  38.    </div><!– #site-info –>
  39.    
  40.   </div><!– #colophon –>
  41.  </div><!– #footer –>
  42. </div><!– #wrapper –>
  43. </body>
  44. </html>

Paste this code into your text editor and save it somewhere handy. We’ll be using it later when we build the file structure for our WordPress Theme. But before we do that, there are a few things we’ll want to take a look at here.

A Quick Tour Through Your WordPress Theme HTML

Firstly, the class attribute on the wrapper, hfeed. hfeed is part of the hatom Microformat schema. In plain English, this means that adding that hfeed class to our page tells any machines reading our site (like search-engine bots or some other service) that our site publishes syndicated content, like blog posts. You’ll be seeing a lot of class names like this as we go along.

Looking at the div structure for the header and footer you’ve probably noticed what I’ll call an inner-outer structure. This is one of the few places (hopefully) that our structure could potentially be accused of Divitis. Potentially. Borrowing class names from the publishing world (WordPress makes us content publishers, right), I’ve tried to add some meaning to the markup that will be resting in these containers. Markup that we’ll be adding in this tutorial series and markup that will may be added in the future. Plus, to accomplish certain layouts, we’ll need this markup structure. It’s better to add it now and do it right.

In the main area of our HTML you’ll notice that we have 2 widget areas that come after our content area. And you may also have noticed that our content rests inside a container div. These points are key. This not only let’s our main content come before the sidebars in the eyes of search engines (and screen readers) but by using a CSS technique involving negative margins we can make this into a 1, 2, or 3 column theme with only a few lines of CSS.

This HTML structure is going to future-proof your WordPress Theme and give you the opportunity to do powerful stuff with CSS. It’s a good one.

How To Create a WordPress Theme

This post is part of a WordPress Themes Tutorial that will show you how to create a powerful WordPress Theme from scratch. Read it from the beginning and code yourself up something awesome.

  1. WordPress Theme Tutorial Introduction
  2. Theme Development Tools
  3. Creating a Theme HTML Structure
  4. Template and Directory Structure
  5. The Header Template
  6. The Index Template
  7. The Single Post, Post Attachment, & 404 Templates
  8. The Comments Template
  9. The Search Template & The Page Template
  10. The Archive, Author, Category & Tags Template
  11. The Sidebar Template
  12. Reset-Rebuild Theme CSS & Define Your Layouts

Don't forget: You should follow me on twitter here.

Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

19 Comments

  1. Posted June 24, 2009 at 7:31 am | Permalink

    Thanks Ian. Excellent tips.
    Your series is shaping up nicely :) (or theme-shaping up nicely)
    You’ve reminded me to try and be leaner and meaner with my own markup and the hatom tip was new to me.
    The expertise you’re sharing is much appreciated and I’m looking forward to the next instalment.

  2. Posted June 24, 2009 at 7:41 am | Permalink

    Ian, this is great, thank you. And thank you for directing me to sixrevisions for local wordpress setup. I had xampp already but I couldnt get anyone to show me how to set up the mysql db on localhost in a way that made sense before yesterday. So now I have what I should’ve had from the start which is a local wordpress test site. 1 tutorial in and this series is already priceless.

    Thanks again!

  3. Chris Dornan
    Posted June 24, 2009 at 8:08 am | Permalink

    Elegant, clear and concise. Very much appreciated.

  4. Posted June 24, 2009 at 8:09 am | Permalink

    Great stuff.

  5. Posted June 24, 2009 at 10:36 am | Permalink

    Hey Ian,
    Great series so far. Couple quick things:

    1) Thanks for the tip on the hfeed class. I wasn’t sure what that was for until now and haven’t included it in my latest work. I will be sure to go back where I can to add it and include it in future work.

    2) I’m still not sure why you are including the #container div, but I am sure you’ll explain it soon. I just don’t yet see that it’s necessary.

    Once again, great start to a very promising series.

    • Posted June 24, 2009 at 11:03 am | Permalink

      If you have the #container div you can create unbreakable layouts with 1 or 2 of the widget areas on the left of the layout—without changing any of the markup. Without it, not so unbreakable.

  6. Posted June 24, 2009 at 1:47 pm | Permalink

    Ian, You said

    but by using a CSS technique involving negative margins we can make this into a 1, 2, or 3 column theme with only a few lines of CSS.

    Targeting a 3-column layout, does the HTML and our CSS we will provide allow for sidebar placement where ever we want them?

    JIM

    • Posted June 24, 2009 at 1:50 pm | Permalink

      Essentially, yeah. I wouldn’t have it any other way. :) But we won’t be running through any CSS until the last tutorial.

  7. Posted June 24, 2009 at 2:50 pm | Permalink

    Hi Ian, great start here. I’d like to make one suggestion if I may? In the interest of those users who may be looking at your HTML structure above and are having trouble visualizing, maybe a graphic representation of these divs would be a helpful addition?

    Just a thought.

  8. Posted June 30, 2009 at 2:30 pm | Permalink

    I have been browsing throught your site. I find it very informative and very helpful. I just started to learn html, very helpful post.
    thank you :)

  9. Posted July 8, 2009 at 5:26 am | Permalink

    “You have a band of thoroughly stuff here.This post is brilliant. You have a real good knowledge about this subject. The way you wrote very nice.

  10. Posted July 9, 2009 at 2:13 am | Permalink

    Markup is really very important and you explained it really well Ian. The markup I used for Archetype is based from LayoutGala’s 40 layouts. But I only chose to write CSS for I think 10 different layouts. I noticed that we have more or less the similar structure. Looking forward to reading every tutorial in this series. Thanks a bunch.

  11. Posted July 14, 2009 at 11:42 am | Permalink

    Just wanted to come by and thank you for making and maintaining this essential plugin. I’ve featured it on my list of plugins I use here: http://www.squidoo.com/wordpress-plugins-thanks

  12. Posted August 1, 2009 at 9:27 am | Permalink

    Amazing , thanks for your such excellent tips.

  13. Posted August 4, 2009 at 9:21 pm | Permalink

    I think this is exactly what I need! Thanks! And I’m glad I came back to check on the website! :)

  14. Posted August 18, 2009 at 2:00 am | Permalink

    Hey Ian!

    Are there any restrictions on the markup we chose? Like certain DIVs must be named so that functionality doesn’t break. We do have such restrictions in bbPress & I don’t know about WordPress. Also can you please highlight more (if there are any) things like “hfeed”?

    Thanks!

  15. Posted November 21, 2009 at 12:39 pm | Permalink

    I’m new to Wordpress and so glad I came across your tutorial – so much goodness here! Love the succint, relevant and passionate explanations. Learning tons, too much to enumerate, and wished I’d come across this earlier…

    Thanks Ian!

  16. Posted January 27, 2010 at 9:12 am | Permalink

    hmmm, Its a great tutorial for wordpress beginners

  17. MadtownLems
    Posted January 29, 2010 at 10:49 am | Permalink

    Great beginner tutorial. I’m looking for some info on adding Theme Options to a theme. Such as:
    Choose which pages you want displayed in these tabs. (instead of just using all of them)
    Choose
    I can do this with a ton of custom php and html, but there’s got to be a better way, right? A plugin, framework, -something-? I tried Thematic, and, while cool, it gave me 13 widgetizable areas, but zero theme options.
    Thanks!

6 Trackbacks

  1. By links for 2009-06-24 | Links | WereWP on June 24, 2009 at 9:04 am

    [...] Creating a WordPress Theme HTML Structure Today comes another tutorial from the series on themeshaper.com. This HTML structure is going to future-proof your WordPress Theme and give you the opportunity to do powerful stuff with CSS. (tags: WordPress tutorial themes) Leave a Reply Click here to cancel reply. [...]

  2. [...] posted here: Creating a WordPress Theme HTML Structure Share and [...]

  3. [...] 原文:Creating a WordPress Theme HTML Structure [...]

  4. [...] Creating a Theme HTML Structure [...]

  5. [...] Read the original post:  Creating a WordPress Theme HTML Structure [...]

  6. [...] 原文:Creating a WordPress Theme HTML Structure [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. In addition, you may find yourself fitter, happier and more productive. Comment away.

Subscribe without commenting