6

Precondition: coming from a Custom PHP applications(using symfony, zend and so on) world, for me it feels unnatural to have template separated in a way that is done in original wordpress theme. e.g top part of a page is in header, center in a "index, page..." and bottom part in a footer.

example of current structure: index.php

[include header]
... main content...
[include footer]

and header.php

<html>
    <head>
      ....
    </head>
    <body>
      .....

and footer.php

       .....
   </body>
</html>

I am more used to the structure where you have a layout and include other (partials) template inside it. example: index.php template

    <html>
      <head> [include header] </head> 
      <body> 
          [include body] [include    footer] 
      </body> 
    </html>

So I am trying to think about advantages of original wordpress template structure.

Question: What are the ADVANTAGES of ORIGINAL wordpress template structure in contrast to the SECOND example I described above ?

2 Answers 2

7

First, as @Toscho implies, the get_header() and get_footer() template tags simply include files named header.php and footer.php, respectively. Both template tags are called from within template files, and you can separate content between template file (index.php) and template part file (header.php, footer.php) any way you want. So, WordPress certainly can accommodate your latter example.

That said, the real power in the standard, "WordPress" method of separating content derives from the WordPress Template Hierarchy. In WordPress, the index.php file is actually the last template file used. It is the default fallback template file when no other, more-specific template file is found:

WordPress Template Hierarchy

Since any one of 7 (primary) to 16 (secondary) template files (and that's just for a publicly distributed Theme; custom-designed Themes can have any number of template files) will be used, depending on the current context, the "WordPress" method of markup separation minimizes the amount of duplicated code.

Let's say you want to change your doctype from transitional to strict (or you want to move to HTML5), or that you want to change your copyright statement in your site footer. Using the "WordPress" method of markup separation, you make those changes in one file. Using your latter method of content separation, you make those changes seven to sixteen (or more) times.

Edit

I figured this question would come up:

I want to hear your opinion concerning following situation. Let's say we need to include "og tags (Open Graph meta tags)" and they have different format for "index", "single.php" "page" "archive" templates. If we have a structure like "original wp theme structure" then we have to use conditional statements(if..else) in the "header.php" file, right ? And I think everybody agrees less "conditional statements" better performance per request, right ?

We have several options:

  1. Use PHP query conditionals directly inside header.php
  2. Use PHP query conditionals in callbacks, hooked into wp_head (or wp_enqueue_scripts, etc.)
  3. Use get_header( $context ), where $context is based on the template hierarchy, and which will include header-$context.php if found, or header.php if not found.

But the multiple options don't really address your underlying question regarding performance hit. Personally, I think the performance hit is negligible, because all of the query conditionals are cached as part of the query object cache, anyway. So calling them really shouldn't incur any meaningful performance hit.

4
  • Chip thanks for the answer I like it a lot! I want to hear your opinion concerning following situation. Let's say we need to include "og tags (Open Graph meta tags)" and they have different format for "index", "single.php" "page" "archive" templates. If we have a structure like "original wp theme structure" then we have to use conditional statements(if..else) in the "header.php" file, right ? And I think everybody agrees less "conditional statements" better performance per request, right ?
    – simple
    Apr 6, 2012 at 18:31
  • So do you think this small performance optimization is worth time that is needed to update multiple files instead of one ?
    – simple
    Apr 6, 2012 at 18:32
  • 1
    Conditional output should be hooked to 'wp_head' per custom function. Template files should be kept a simple as possible.
    – fuxia
    Apr 6, 2012 at 18:48
  • See edited answer; I don't think there is any meaningful performance hit for using query conditionals. Apr 6, 2012 at 19:15
7

There is no contradiction, you can use both.

Example

<!doctype html>
<html <?php language_attributes(); ?>>
  <head>
    <?php get_header(); ?>
  </head>
  <body>
    <?php get_template_part( 'content', get_post_format() ); ?>
    <?php get_footer(); ?>
  </body>
</html>

The advantage of the WordPress system is: You are free to structure your templates as you wish.

1
  • I am really interested why most of the templates I see use the first original structure. Since I am very concerned about performance, I am interested if original structure if somehow efficient in terms of performance!
    – simple
    Apr 6, 2012 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.