• Resolved kreegah

    (@kreegah)


    I’ve been trying out for hours now, and I’m getting frustrated!

    I want to change my css according to what custom taxonomy my page shows, this is my code so far:
    `<?php
    function my_enqueue_scripts() {
    wp_enqueue_style( ‘style’, get_stylesheet_uri(), ”, ‘1.0’, ‘screen’ );
    }
    add_action( ‘wp_enqueue_scripts’, ‘my_enqueue_scripts’ );

    function change_stylesheet_uri( $stylesheet_uri, $stylesheet_dir_uri ) {

    // see in_category for examples at https://codex.www.ads-software.com/Conditional_Tags

    if ( taxonomy_exists( ‘product_categories’, ‘grunnmur’ ) )
    return $stylesheet_dir_uri .’/grunn.css’;

    elseif ( taxonomy_exists( ‘product_categories’, ‘gulv’ ) )
    return $stylesheet_dir_uri .’/gulv.css’;

    else
    return $stylesheet_dir_uri . ‘/style.css’; // our default stylesheet

    }
    add_filter( ‘stylesheet_uri’, ‘change_stylesheet_uri’, 10, 2 );

    ?>’

    And this works as it uses the custom css /grunn.css for product_categories, but /style.css for my other pages. But I want it to use /grunn.css for the product_categories ID/-or term which in this case is ‘grunnmur’ because I want ‘gulv’ to use another color (hence another css-file) as of now ‘gulv’ uses /grunn.css aswell.

Viewing 7 replies - 16 through 22 (of 22 total)
  • I think this is what you want ( replace the comment with the return for your stylesheet ):

    function change_stylesheet_uri( $stylesheet_uri, $stylesheet_dir_uri ) {
    
        if (is_tax( 'product_categories', 'grunnmur' ))
        	return $stylesheet_dir_uri .'/grunn.css'; 
    
        elseif (is_tax( 'product_categories', 'gulv' ))
        	return $stylesheet_dir_uri .'/gulv.css'; 
    
    	elseif (is_tax( 'product_categories', 'vegg' ))
       	return $stylesheet_dir_uri .'/vegg.css'; 
    
    	elseif (is_tax( 'product_categories', 'tak' ))
        	return $stylesheet_dir_uri .'/tak.css'; 
    
    	elseif ( get_query_var('post_type') == 'products' )
    	   // Return the desired stylesheet;
    
     	else
     		return $stylesheet_dir_uri . '/style.css'; // our default stylesheet
    
    }
    Thread Starter kreegah

    (@kreegah)

    outputs

    Parse error: syntax error, unexpected T_ELSE in line 31

    =/

    Thread Starter kreegah

    (@kreegah)

    my bad, I didn’t add a return

    But this doesnt force my products to use “grunn.css” if the product is listed in “grunnmur”, this will just override all the above code for every products to the set return stylesheet wouldn’t it?

    Did you replace the comment line with your own return statement?

    Thread Starter kreegah

    (@kreegah)

    You should have received a mail with the website in question so that you can see what the “issue” is. (i don’t want the url published on these forums)

    ??

    I am having a hard time understanding the full scope of what you want.

    Please list all the different URLs and what you want to do for each one.

    Thread Starter kreegah

    (@kreegah)

    Just to clarify vtxyzzy helped me fix my problem. posting the finalized code here if anyone else needs this kind of solution:

    // Custom CSS per Category and postname
    
    function my_enqueue_scripts() {
    
       wp_enqueue_style( 'style', get_stylesheet_uri(), '', '9.0', 'screen' );
    
    }
    
    add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
    
    function change_stylesheet_uri( $stylesheet_uri, $stylesheet_dir_uri ) {
    
       if ( is_single() ) {
    
          global $post;
    
          $term_list = get_the_term_list($post->ID,'product_categories');
    
       }
    
       if (strpos($term_list,'sitea') || is_tax( 'product_categories', 'grunnmur' ))
    
          $file =  $stylesheet_dir_uri .'/sitea.css';
    
       elseif (strpos($term_list,'gulv') || is_tax( 'product_categories', 'gulv' ))
    
          $file =  $stylesheet_dir_uri .'/gulv.css';
    
       elseif (strpos($term_list,'siteb') || is_tax( 'product_categories', 'vegg' ))
    
          $file =  $stylesheet_dir_uri .'/siteb.css';
    
       elseif (strpos($term_list,'sitec') || is_tax( 'product_categories', 'tak' ))
    
          $file =  $stylesheet_dir_uri .'/sitec.css';
    
       else
    
          $file =  $stylesheet_dir_uri . '/style.css'; // our default stylesheet
    
       return $file;
    
    }
    
    add_filter( 'stylesheet_uri', 'change_stylesheet_uri', 10, 2 );
Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘taxonomy and term function’ is closed to new replies.