Use More Flexibility In WordPress Templates

Those who have wanted to create their Theme for WordPress quite flexible, and loves the modularization, had always to involve the path to the template. With WordPress 3.0, this will be different and it is possible to use a function, which makes it easy to load the template.

An example

Let us imagine that you will use a menu several times - but at different positions and at different places of the markup. It would not be sufficient if you change the appearance via CSS. In most cases you would probably put the navigation in an additional template, such as the nav.php. In this file is the HTML markup and the necessary template tags to create a navigation. This file would be included. In WordPress 3.0 it looks like this: include( TEMPLATEPATH . '/navi.php' );

Function get_template_part()

With WordPress 3.0 comes in this form of modularization more flexibility and so you can use the get_template_part() to address templates. The function allows the use of two parameters:

/**
 * @uses locate_template()
 * @since 3.0.0
 * @uses do_action() Calls 'get_template_part{$slug}' action.
 *
 * @param string $slug The slug name for the generic template.
 * @param string $name The name of the specialised template.
 */
get_template_part( $slug, $name = null )

and it has a hook get_template_part_$slug, which you can use.

Let's take a look on the example with the navigation above, which will be include in the new function as follows: get_template_part( 'navi' );. In this case we only used the parameter slug - navi - which takes care, that it loads navi.php.
In our next step we want to use the navi, which was created explicitly for the sidebar. The source is in the navi-sidebar.php and the call will look like that: get_template_part( 'navi', 'sidebar' );. The structure of the template file must be created by slug-name.php .

This allows to create almost any pattern, which are becoming especially valuable when it comes to reuse, or if you work with Child-Themes, as you can thus connect to the Parent-Theme and yet create your own quite flexible templates.

Sequence loading

WordPress uses when loading the template a hierarchy, so you can access various templates, that can be illustrate in an example. The Parent-Theme is in wp-content/themes/ and uses the folder wpbasis, the Child Theme can be found in the folder wpbasischild. Now there is in one of the templates the call <?php get_template_part( 'navi', 'index' ); ?>. Now it searches after the navi-index.php with the following pattern:

  1. wp-content/themes/wpbasischild/navi-index.php
  2. wp-content/themes/wpbasischild/navi.php
  3. wp-content/themes/wpbasis/navi-index.php
  4. wp-content/themes/wpbasis/navi.php

Here, again, some new features under the hood, which can be quite useful for theme authors. Because of its recent growth, the use of Child-Themes is certainly a useful feature.

Comments are closed.

7 comments

  1. Deluxe Blog Tips

    Using the function get_template_part() can help us to avoid duplication in wp theme code. But when I wrote a similar post about this function, I received some opinions that said this is for simple or not very big wp site.

  2. curtismchale

    Unless there is a way to organize these files inside folders I'd have to agree that this is only for smaller sites with one or two items abstracted. I can only imagine how cluttered a theme would get with lots of template parts floating around.

  3. Frank

    @curtismchale: no, the functions has not a change for folders ๐Ÿ™ You can use locate_template() for this work.

    @Deluxe Blog Tips: yes, i see this also, my navi is only an example and the rest is the idea from the developers of a theme

  4. Rami

    See also "get_template_part()" on wordpess codex:
    http://codex.wordpress.org/Function_Reference/get_template_part

  5. John

    Thanks for the info Frank, I seem to find myself on this site often =) Can you expand on using locate_template() to make it possible to allow the use of get_template_part() when using sub-folders in the theme?

2 pingbacks

  1. Refactor or Retire? ICE and WordPress 3.0 - Idealien Studios
  2. Best On WordPress From The Past Week N.5 ยป wpCanyon