Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On one of our latest projects I'm planning to use the index.php file to display different taxonomy layouts. Basically I'm planning to use is_tax( 'sometaxonomy' ) to display the data/loop for different taxonomies. Are there any performance concerns I should address? Is it better to use taxonomy-$taxonomy.php in this case? if so, why?

Also, as a somewhat related issue: when using this technique I will need to customize the loop. Is it safe (performance-wise) to use global $query_string to alter the query? Or should I, again, simply use taxonomy-$taxonomy.php?

Thanks in advance

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

TL;DR Template: Performance not so much, semantics however, use hierarchy!

TL;DR Query String: Use provided hooks, never edit directly.

The Template File

Apart from performance, first look at semantics. If you direct your browser to a taxonomy archive, WordPress will look for the following files (straight from the Codex):

  1. taxonomy-{taxonomy}-{term}.php - If the taxonomy were sometax, and taxonomy's term were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is 'post_format' and the terms are 'post-format-{format}'. i.e. taxonomy-post_format-post-format-link.php
  2. taxonomy-{taxonomy}.php - If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php
  3. taxonomy.php
  4. archive.php
  5. index.php

So it seems WordPress has a system to handle your challenge. The question is, if WordPress provides an internal fallback template hierarchy system where taxonomy.php could be used to display all taxonomy pages and taxonomy-{taxonomy}.php displays that specific taxonomy.

You are creating this for just yourself? Use whatever you like. The lookup system is quite fast so don't worry about performance. The conditional tags are pretty quick as well. But you probably are not. Let's see why not to use index.php:

  • WordPress provides functionality to display taxonomies.
  • WordPress already queries for taxonomies, so why re-query it because you want to use a page initially not designed for taxonomies?
  • Someone else might look over the code, needing to look up 1, 2, 3 and 4 of the hierarchy before realising the code is in index.php. That someone might be you after a decent amount of time.
  • Clutters index.php unnecessarily.
  • You don't need to customize the loop in taxonomy.php.

The Query String

Never change the query string directly, but use the hooks provided to change the query string such as pre_get_posts. The Codex page is quite extensive. All related hooks are listed as well (such as join statements and order statements). Just never change the loop directly. Why? Because WordPress provides. Plugins might hook into this too and several other reasons.

share|improve this answer
    
Thank you. I'm familiar with the template hierarchy, but I actually like the way all the different archive layouts are in the same place. I really don't think it's cluttering, on the contrary. But that's just me. I think that my question really is: what is faster: the fallbacks or the statements. Great call on pre_get_posts! don't know what I've been thinking :) –  Daniel Sachs Jan 16 '13 at 16:24
    
@DanielSachs Haha, but if all the archive layouts are the same or have to be in the same place, at least try to get up the tree with archive.php. Nevertheless, you could always implement get_template_part( 'content', 'some-archive' ) or something similar. Yes, a lot of people don't know about it, but it's kinda vital. As a rule: if there is a $variable (not $_variable) there is [almost always] a hook of filter. That's in the coding guidelines of the WP Core ;) (Almost always: en.wikipedia.org/wiki/Almost_surely) –  Derk-Jan Jan 16 '13 at 16:33
1  
LOL'd at Almost surely! :) ... on the other issue - I'm actually implementing get_template_part('templates/content', get_post_type()); and in index I add specific search boxes, ajax select boxes etc. for specific post types, taxonomies. I'm creating a loop directly in index.php only for one specific taxonomy. BTW - just changed the query_posts atrocity I made to pre_get_posts and now the pages are freakishly fast - it seems like there is no loading time at all. Thanks! –  Daniel Sachs Jan 16 '13 at 16:59
1  
You are very welcome! The reason is that you effectively cut two queries per page load (the ones you were overwriting). I suggest looking at slideshare.net/andrewnacin/… if you want to understand why it became so much faster. –  Derk-Jan Jan 16 '13 at 17:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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