0

I want to change the css style of a single.php which belongs to a custom taxonomy, based on the category. I achieved to do it in taxonomy.php for some of the categories but i can't make it to work on a single.php. For the taxonomy.php i used is_tax and worked ok. For single.php i used has_term but nothing.

What am i doing wrong? Both of the files full code are here and here

thank you

Here is the taxonomy.php which everything worked great.

<?php get_header(); ?>
<div class="non-home-parallax product-listing-parallax" style="
<?php if( is_tax ('product-cat', 'fama-advance') ) { ?>
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/advance.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-premium') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/premium.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-confectionary-solutions') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/confectionary.jpg'); 
<?php }
elseif ( is_tax ('product-cat', 'fama-powder-solutions') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/powder.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-improvers') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/improvers.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-chocoday-creams') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/chocodaycreams.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-choco-day') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/chocoday.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-deliciozo') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/deliciozo.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-frutopia') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/frutopia.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-fruigel') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/fruigel.jpg'); 
<?php } 
elseif ( is_tax ('product-cat', 'fama-creams') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/creams.jpg'); 
<?php }  
elseif ( is_tax ('product-cat', 'fama-ice') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/ice.jpg'); 
<?php }  
else { ?> <?php } ?> ">
        <div class="container-12">
            <div class="col-12">
                    <h4 style="
                    <?php if( is_tax ('product-cat', 'fama-advance') ) { ?>
                     background-color: #ff9900; 
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-premium') ) { ?> 
                    background-color: #004A93; 
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-confectionary-solutions') ) { ?> 
                    background-color: #E7C388;                    
                    <?php }
                    elseif ( is_tax ('product-cat', 'fama-powder-solutions') ) { ?> 
                    background-color: #FFED3D;                    
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-improvers') ) { ?> 
                    background-color: #FDBA2F;                    
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-chocoday-creams') ) { ?> 
                    background-color: #693A18;                    
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-choco-day') ) { ?> 
                    background-color: #3B210D;                    
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-deliciozo') ) { ?> 
                    background-color: #DB0070;                    
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-frutopia') ) { ?> 
                    background-color: #98005D;                    
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-fruigel') ) { ?> 
                    background-color: #BB001C;                    
                    <?php } 
                    elseif ( is_tax ('product-cat', 'fama-creams') ) { ?> 
                    background-color: #96C031;                    
                    <?php }  
                    elseif ( is_tax ('product-cat', 'fama-ice') ) { ?> 
                    background-color: #9FDAF1;                    
                    <?php }  
                    else { ?> <?php } ?> ">
                    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h4>

            </div>
        </div>

And here is some code of the single.php

<?php get_header(); ?>
<div class="non-home-parallax product-listing-parallax" style="
<?php if( has_term ('fama-advance', 'product-cat') ) { ?>
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/advance.jpg'); 
<?php } 
elseif ( has_term ('fama-confectionary-solutions', 'product-cat') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/premium.jpg'); 
<?php } 
elseif ( has_term ('fama-premium', 'product-cat') ) { ?> 
 background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/confectionary.jpg'); 
<?php } 
else { ?> <?php } ?> ">

1 Answer 1

1

has_term must be used inside the loop. If you use it outside the loop you have to pass the post ID or post object you want to check.

In the single.php file put your code inside the loop to check the current post:

<?php get_header();

while(have_post) {
    the_post();
    ?>
    <div class="non-home-parallax product-listing-parallax" style="
     <?php if( has_term ('fama-advance', 'product-cat') ) { ?>
          background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/advance.jpg'); 
     <?php } elseif ( has_term ('fama-confectionary-solutions', 'product-cat') ) { ?> 
          background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/premium.jpg'); 
     <?php } elseif ( has_term ('fama-premium', 'product-cat') ) { ?> 
          background-image:url('<?php echo get_template_directory_uri(); ?>/img/parallax/confectionary.jpg'); 
     <?php } else { ?> <?php } ?> ">
           The rest of you code
 <?pphp } //end while
 get_footer(); ?>

Also, I recommend to you to add a different class instead of a different style attribute and put the style in you CSS file. Also, it would be better to define a function that return the class instead of that ugly set of if else statements in the template file. Even better, use the post_class function in the container of your post. This function returns a string of class that you can filter to include classes based on your custom taxonomy.

In the single.php template file:

<?php get_header();

while(have_post) {
    the_post();
    ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
           The rest of you code
    </div>
 <?pphp } //end while
 get_footer(); ?>

In the functions.php file:

// add product-cat in post class
add_filter( 'post_class', 'product_cat_class' );
function product_cat_class( $classes ) {
     global $post;
     $product_cats = get_the_terms( $post->ID, 'product-cat' );
     foreach ( $product_cats as $product_cat ) {
         $classes[] = $product_cat->slug;
     }
     return $classes;
 }

In your CSS, for example:

.fama-advance {
          background-image: url('img/parallax/advance.jpg');
}
3
  • 1
    Sir, your answer was absolutely right. Thank you very much phrase.it/image/rendered/ykdngz.jpg
    – hambos22
    Aug 17, 2014 at 0:58
  • only one problem though.. For the taxonomy.php. Now that i learnt how to use the post_class, i want to use it to taxonomy.php to get rid of the ugly code. I must use the get_the_terms to print the slug of the current category as class?
    – hambos22
    Aug 17, 2014 at 1:17
  • 1
    nevermind i used this <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->slug; ?> inside class . Thank you again :)
    – hambos22
    Aug 17, 2014 at 1:24

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.