0

My question has a couple of aspects:

My current custom theme is fairly vanilla, with a homepage, tag/tax index page and content page templates.

I want to be able to have a couple of custom layout templates for tag pages, so rather than the template hierarchy simply loading the template php, I want to:

  • check to see if this tag id/ slug has a custom layout
  • if it does, load it, with its custom layout elements
  • if not, load the default template

My site is quite high traffic so I'm looking for a solution that doesn't have a huge performance overhead. Ideally at the point of determining if we're loading something custom, I want to know if Wordpress makes available the tag/tax ID or slug so I can do a nice quick lookup on my custom table.

  • 1
    template_include filter plus get_queried_object_id() plus locate_template ;-) – Pieter Goosen Oct 12 '15 at 7:01
1

You can use the tag_template or taxonomy_template filters and check the queried object from within that, then use locate_template to get the template path.

function wpd_tag_template_filter( $templates = '' ) {
    $this_tag = get_queried_object();
    // check $this_tag->term_id, $this_tag->slug, etc.
    $templates = locate_template( 'my_custom_template.php', false );
    return $templates;
}
add_filter( 'tag_template', 'wpd_tag_template_filter' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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