multiple conditions for custom taxonomies template

Hello,
I have created multiple taxonomies and I am trying to have a different template in single post for these taxonomies. The code I use in content-single.php is:
`<?php if(has_term('body','massage-type')) {
get_template_part('content-single', 'taxonomy');
}
elseif(has_term('facial','massage-type', $post)) {
get_template_part('content-single', 'taxonomy');
}
else { // use default template file single-template.php
get_template_part('content-single-general');} ?>`
but I cannot insert more than these 2 taxonomies because the code brakes. How could I insert more if or elseif in the code without braking it? thank you!!

  • Ioannis
    • The Incredible Code Injector

    Hi Jude,

    the code is

    <?php
    
    if(has_term('body','massage-type')) {
    
      get_template_part('content-single', 'taxonomy');
    }
    elseif(has_term('facial','massage-type', $post)) {
    
      get_template_part('content-single', 'taxonomy');
    }
    
    else {
    
      get_template_part('content-single-general');
    }
    ?>

    but if I add more elseif it brakes. Could you please give me an example? Thank you!!

  • Ioannis
    • The Incredible Code Injector

    Hello Jude,

    I am sorry but the only code that works is the initial code that I gave you. It works only for the first if statement and not for the next elseif. What else could I try? I'll give you an example.I have 2 taxonomies that are "massage-services" that include "facial-massage","body-massage" etc and "Cities" taxonomy that may include "London",Paris","New York" etc. I need single posts in this two taxonomies to have different template from any other post in the site. Special template for these taxonomies: "content-single.php" and template for any other post: "content-single-general.php". I wish there is away that I could achieve that..

  • Ioannis
    • The Incredible Code Injector

    I finally found out the right code. The problem was the word "$post" in the else if line. So if anyone else tries to make a different template for single posts for taxonomies here are the steps:

    1) Create a child theme otherwise at the next update of your theme You will loose all the changes You have done. https://premium.wpmudev.org/blog/how-to-create-wordpress-child-theme/

    2) Copy your single.php in your child theme, make the changes You want and rename it single-taxonomy.php. This template will apply to single posts of taxonomies that You will add in the file We will create later.

    3) Copy your single.php again in Your child theme and rename it single-general.php

    4) Now create a new file name it single.php and add the following code in it:

    <?php
    
    if(has_term('your-taxonomy-term-slug','slug-of-your-taxonomy')) {
      // use template file single-taxonomy.php
      get_template_part('single', 'taxonomy');
    }
    elseif(has_term('another-taxonomy-term-slug','slug-of-your-taxonomy')) {
      // use template file single-taxonomy.php
      get_template_part('single', 'taxonomy');
    }
    //add as much elseif You want. You may add else if for categories too with same template or with a different one.
    
    else {
      // use default template file single-general.php
      get_template_part('single-general');
    }
    ?>

    Every time that You add a new term in Your taxonomy, do not forget to add a new else if with this term in this file.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.