/ coding

Abstracting repeated WordPress functionality to a custom template

While developing Strips, the WordPress theme that I’m writing and that this site is currently using, I’ve noticed that I’ve been moving and revising how the post categories are displayed rather often.

The thing is that this movement and revision never affects only one template in the theme: any template that uses categories, like home.php and single.php, is affected so I find myself doing the same work several times.

Not only is that needlessly time-consuming, but I have to wonder if I’ve moved everything that needs to be moved, and if the revision has been typed in correctly each time.

This burden could be greatly lessened by abstracting the category display functionality to its own template.

As far as I can tell, category.php is intended to be an archive template for categories, not a template for category display functionality. You can read more in the WordPress Codex.Since category.php is an officially sanctioned template file name in WordPress, it should be used as directed. So instead, I made a completely different file name: post-category.php. This name could be anything really, as long as it doesn’t interfere with any file names in the Template Hierarchy.

In my new post-category.php file, I simply pasted the code that displays the categories, and then removed that code from all other templates. ```

’) ?>

You may note that this also includes code for the date, but since the date is displayed with the categories in Strips, it belongs here too.

So to recap thus far, I’ve:1. Made a new template file called post-category.php
2. Pasted in all code related to displaying the categories (and date)
3. Removed the category display code from all other templates

If I stop here, the effect is that categories are not displayed in the theme at all. While there is a template for category display (post-category.php), none of the other templates are interacting with it.

Now to fix that.

Anywhere I want to display categories in the theme, all I need to do is enter one line of code:```

There are 3 arguments in `locate_template()` above:

1. ’post-category.php’ (the file name for the template to be displayed)
2. true (tells the theme to load the template)
3. false (tells the theme that the template can be loaded more than once, which is needed since I am using `locate_template()` in The Loop)

You can read more about `locate_template()` on the [WordPress Codex](http://codex.wordpress.org/Function_Reference/locate_template "locate_template on the WordPress Codex").

- - - - - -

### Pros

This approach makes developing simpler since there is only one instance of code for displaying categories. If I want to change how categories are displayed, I have one stop: `post-category.php`.

Also, if I want to change *where* the categories are displayed, I only have one line of code to move around, mitigating possible copy/paste mistakes in multi-line code.

### Cons

I need to learn more about what affect this type of abstraction has on performance.

For example, to show a single blog post on its permalink, 2 templates must now be loaded: `single.php` and `post-category.php`. (Of course there are more templates, like header and footer, which are always being used as well.)

I’d like to find out how the addition of one more template to load affects the performance of the theme.

### Conclusion

For now, I think this is a great way to work during development of the theme. Changes are easier to implement and the location of functionality is easier to understand.

If I find that performance is taking a noticeable hit, it could be that when I’m ready to publish version 1.0 of Strips, that I un-abstract the functionality.

But while in development, this has proven to be an unquestionably useful solution.

Abstracting repeated WordPress functionality to a custom template
Share this