0

I have a custom post type, service-provider, with two taxonomies, area and service. I wanted to make a template based on the taxonomies.

function template_taxonomy($template) {
    if (is_archive()) {
        if ((is_tax('service') and is_tax('area')) or is_tax('service')) {
            $template = locate_template( array( 'taxonomy_service.php' ) );
        } else if (is_tax('area') and !(is_tax('service'))) {
            $template = locate_template( array( 'taxonomy_area.php' ) );
        }
    }
    return $template;
}

The code is pretty simple but my problem is, when I try to visit this URL

http://www.site.com/?service-provider&service=electricians&area=north-shore-lower

the service taxonomy doesn't seem to exist, so it uses the taxonomy_area.php template. Also tried just using the taxonomy_service.php template and it seems like the area taxonomy doesn't exist on that template either.

Any ideas?

  • That template is going to be used to display the taxonomy archive or a post? When and where do you call the function in the code you have posted? Also, the URL you have posted is wrong, notice that the service-provider query string has no value. – cybmeta May 26 '14 at 18:55
  • Also, take a look to template_inlude entry in the codex, you can create a template file for each taxonomy, with the name taxonomy-$taxonomy-slug.php, and Wordpress will load it automatially. – cybmeta May 26 '14 at 19:04
  • I do use template_include. It's in functions.php of a child theme. I need to to display it in the archives. – xporj May 27 '14 at 1:52
  • And I'm not sure about the URL being wrong. It displays what I want to display, just using the wrong template. – xporj May 27 '14 at 2:25
  • If the URL is giving you what you want maybe you don't need to have service-provider in the URL because that query string has no value in the URL you posted. Anyway, I ask you again: What do you want in that page? A list of post of "service-provider" type that belongs to service=electricians & area=north-shore-lower? A single post? Anything else? – cybmeta May 27 '14 at 7:12
0

If you are trying to display a list of post that belongs to two or more taxonomy terms, you are using the wrong URL. If your are usgin pretty permalinks, it should be:

 www.example.com/service-provider/?service=electricians&area=north-shore-lower

If you are not using pretty permalinks, it should be:

 www.example.com/?post-type=service-provider&service=electricians&area=north-shore-lower

Anyway, you are expected to be in multitax archive page and you can't check it with is_tax(), a function intended to check for single taxonomy archive. You can use some alternatives, like checking the actual $wp_query object:

add_filter('template_include', 'wpse_multietax_template');
function wpse_multietax_template( $template ){

    global $wp_query;

    if( (isset($wp_query->query_vars['service']) && isset($wp_query->query_vars['area'])) || isset($wp_query->query_vars['service']) ) {
         $template = locate_template( array( 'taxonomy_service.php' ) );
    } else if ( isset($wp_query->query_vars['area']) && !isset($wp_query->query_vars['service']) ) {
         $template = locate_template( array( 'taxonomy_area.php' ) );
    }

    return $template;
}
  • I tried both URL's because I'm desperate. The first URL gave me a 404 error. The second one still uses the template for the area taxonomy. – xporj May 27 '14 at 8:12
  • Please, see edited answer. – cybmeta May 27 '14 at 14:57
  • Thanks, it works now but I have another problem. So in the template taxonomy_service.php I want to access the terms of each of the taxonomies. I tried using get_term_by('slug', get_query_var('term'), 'service'); but that only seems to work on one taxonomy, namely area, just like my recent problem. – xporj May 27 '14 at 16:13
  • Thats another question. Anyway, get_query_var('term') will try to get the queried term within a taxonomy called "term" but you haven't queried for post in a taxonomy called "term", isn't it? You must use get_query_var('service'), get_query_var('area') or whatever taxonomy name you have queried. See get_term_by() and get_query_var() docs for more info. – cybmeta May 27 '14 at 16:30
  • Thank you very much for your answers. I got it working now. :D – xporj May 27 '14 at 16:55

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.