• 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 15 replies - 1 through 15 (of 22 total)
  • I believe you need to use term_exists() instead of taxonomy_exists().

    Thread Starter kreegah

    (@kreegah)

    Thanks for your reply vtxyzzy, I found this out 5 minutes ago, but
    with my code, it still ignores my ID of child-taxonomy (18 and $gulv )

    if ( term_exists( 18, $taxonomyname ) )
    return $stylesheet_dir_uri .’/grunn.css’;

    elseif ( term_exists( $gulv, $taxonomyname ) )
    return $stylesheet_dir_uri .’/gulv.css’;

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

    Please explain what you mean by ‘child-taxonomy’ and what are the values of $taxonomyname and $gulv?

    Thread Starter kreegah

    (@kreegah)

    Child-taxonomy is my subcategory of my custom taxonomy

    ‘$taxonomyname = “product_categories”; // the custom_taxonomy
    $gulv = “15”; // the id of product_categories gulv (child)
    $grunn = “18”;’ // the id of product_categories grunn (child)

    WordPress still ignores and reads only the taxonomy, and not the ID i want to be found.

    I am still a little confused. Is child-taxonomy a term of a taxonomy?

    Thread Starter kreegah

    (@kreegah)

    Yes, I’m sorry I am also confused, hehe. I’m not a coder at all.

    I got custom taxonomy = product_categories
    In product_categories i have child/subcategory/terms: gulv & grunn

    on my product page when viewing category “gulv” and its products I want it to load gulv.css, and then “grunn” to load grunn.css

    I get the css to load, but only for existing within product_categories, and not the “gulv” og “grunn” category

    I’ve fiddled some more and got this code now

    <?php
    $taxonomyname = "product_categories";
    ...
        if ( term_exists( 'grunn' ) )
        	return $stylesheet_dir_uri .'/grunn.css'; 
    
        elseif ( term_exists( '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 );
    
    ?>

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum’s parser.]

    It looks like the code you posted should work.

    One point, variables declared outside a function are not available inside the function. You must either declare them global in the function, or pass them as parameters.

    So $taxonomyname would not have a value inside the functions.

    Thread Starter kreegah

    (@kreegah)

    Yeah, I’ve removed my variable.

    but unfortunately my code won’t work ??

    function change_stylesheet_uri( $stylesheet_uri, $stylesheet_dir_uri ) {
    
        if (term_exists( 'grunnmur' ))
        	return $stylesheet_dir_uri .'/grunn.css'; 
    
        elseif (term_exists( 'gulv' ))
        	return $stylesheet_dir_uri .'/gulv.css'; 
    
     	else
     		return $stylesheet_dir_uri . '/style.css'; // our default stylesheet
    
    }

    The really weird part is that my url shows

    /?taxonomy=product_categories&term=gulv

    and still the css for term_exists( 'grunnmur' ) loads in the whole page. not just for the product category “Grunnmur” as it should.

    Sorry, I misunderstood what you were trying to do.

    The terms will always exist whether you are showing that term on the page or not. Try using the is_tax() function instead.

    Thread Starter kreegah

    (@kreegah)

    Whooopie! You saved my day vtxyzzy!

    That worked flawlessly!

    (argh, to many functions for a non-coder to handle in this WP codex)

    Thanks a bunch, if you were close by I’d hug you!

    Thread Starter kreegah

    (@kreegah)

    I cheered to soon, the above works perfect for giving my product category different css. But I also have

    ?post_type=products

    to be viewed inside each category. How would I go by to add these into my custom css function.

    if (is_tax( 'product_categories', 'grunnmur' ))
        	return $stylesheet_dir_uri .'/grunn.css';
    // add to also be true in post_type=products&p=ID-OF-PAGES

    If you know how and got the time ??

    Please post your current code.

    Thread Starter kreegah

    (@kreegah)

    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'; 
    
     	else
     		return $stylesheet_dir_uri . '/style.css'; // our default stylesheet
    
    }

    I think you can use get_query_var():

    elseif ( get_query_var('post_type') == 'products' )
       // Return the desired stylesheet;
    Thread Starter kreegah

    (@kreegah)

    So how do I go about to add this in addition to the code I already have?

    I was originally hoping that the earlier (resolved) code would be added to the products aswell, since they are in the different categories of `
    grunnmur, vegg and so on`

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘taxonomy and term function’ is closed to new replies.