0

I want to make template page for my custom taxonomy.

I have custom taxonomy categories listed as navigation links. It's the country list (China, Slovenia, Serbia...). Every of this categories has cities as their subcategories (Belgrade, Honk Kong...).

When i click on the e.g. China i get redirected to index.php Wordpress template file where there is some loop and scripts that show content. That is working great, but i don't want that :)

I want, when i click on the category, to be redirected on my custom template page in which i would make some scripts that would display cities list for that country and when i click on the city i would go to index.php template and show the posts for that city!

I hope you understand whatr i'm looking for! Please help!

1

3 Answers 3

1

Your question implies that index.php is a page to be redirected to, it isn't, it's purely a template file. It implies that single.php being loaded is what makes a page singular, or that loading the page.php template will give you a page, when it's actually the other way around.

I strongly recommend reading about the template hierarchy, it should answer your question, and make a few things about WordPress chooses a template file to load in your theme make a lot more sense.

Specifically WordPress:

  • Uses rewrite rules to match the URL to a set of query variables to pass into a database query
  • Passes those through the pre_get_posts filter then makes the query to grab posts
  • Uses those query variables to choose a template

E.g. if the s query var is present, then is_search() will return true, and WordPress will look for a search.php theme template to load. If it doesn't exist, it loads the fallback, archive.php, and if that doesn't exist it loads the final fallback index.php.

In your custom taxonomy case it will load these files in this order until it finds one:

  • taxonomy-$taxonomy-$term.php
  • taxonomy-$taxonomy.php
  • taxonomy.php
  • archive.php
  • if paged=true: paged.php
  • index.php

Were $taxonomy is the internal name of your taxonomy ( as used in register_taxonomy ), and $term is the ID of the taxonomy term.

Further Reading

0
function myTemplateSelect() {
    if (is_category() ) {

            load_template(TEMPLATEPATH . '/nameoftemplate.php');
            exit;

    }
}

add_action('template_redirect', 'myTemplateSelect');

add this in your functions.php.

replace your template path with TEMPLATEPATH . '/nameoftemplate.php

2
  • It isn't regular category, as i explained above. It's custom taxonomy, and if I manage to redirect with this function it will also redirect on template when i click on the subcategory and i don't want that! I want this redirection just for first level
    – Kuka
    Commented Aug 27, 2013 at 13:17
  • thanks for this solution, but it is bad practice, to use template_redirect for includes. cause after that hook some other filters are running, and they might break the site, if you use your solution. you can run your function with the template_include hook, to change the template.
    – honk31
    Commented Jun 6, 2019 at 13:18
0

You'll need to add template files to your theme as pr1nc3 linked to.

WordPress will automatically load the appropriate file when the taxonomy term is requested.

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.