Changing stylesheet depending on custom taxonomy terms

My client would like the background & header colors to switch
depending on terms within the custom taxonomy ‘Grades’. These terms
are kindergarten, first grade, second grade, etc… (all through
twelfth grade) and are associated with a custom post type ‘projects’.

I’ve set up the menu so that each term in ‘grades’ has it’s own page.
These are not actual pages that you’d find in the dashboard. These
pages are just an archive-type pages specific to each grade.

Read More

I found a great blogpost regarding contextually changing the
stylesheet depending on the page name (
http://justintadlock.com/archives/2009/07/27/contextually-changing-your-themes-stylesheet
). My php understanding is a bit limited and I can’t figure out how
to edit this code so that instead of switching stylesheets because of
a page’s name, it is triggered by an array of terms within the custom
taxonomy ‘grades.’

I want to switch stylesheets on archive pages for custom taxonomies.
The switch should be triggered by a custom taxonomy’s terms. IE: The
Kindergarten page is actually an archive page for ‘kindergarten’ in
the the custom taxonomy ‘grades’ I have 13 terms associated with
‘grades’ > kindergarten, first grade, second grade, third grade,
etc…

IE: I would like the background image and heading colors to change.
Green for kindergarten, first grade, second grade, third grade…
Orange for sixth grade, seventh grade, eighth grade… Purple for
ninth grade, tenth grade, etc…

I found a solution that works only for archive pages and taxonomies: is_tax function.

add_filter( 'stylesheet_uri', 'my_stylesheet', 10, 2 );

function my_stylesheet( $stylesheet_uri, $stylesheet_dir_uri ) {

if ( is_tax( 'grades', array('term_name' => 'Kindergarten', 'First Grade', 'Second Grade', 'Third Grade', 'Fourth Grade', 'Fifth Grade' ) ))
    $stylesheet_uri = $stylesheet_dir_uri . '/style-elemen.css';
elseif ( is_tax( 'grades', array('term_name' => 'Sixth Grade', 'Seventh Grade', 'Eighth Grade' ) ))
    $stylesheet_uri = $stylesheet_dir_uri . '/style-elemen.css';

return $stylesheet_uri;

}

Related posts

Leave a Reply

1 comment

  1. Like Justin Tadlock says in your referenced article, the body_class() provides the ability to add classes dependant on the type of term. Given that you indicate your php knowledge is still growing; this maybe the best solution.

    The codex provides a list of classes on a body_class enabled:

    http://codex.wordpress.org/Function_Reference/body_class

    If that isn’t sufficient; wordpress has a great script/style loader function, wp_enqueue_script to load scripts/styles:

    <?php 
    
    /** 
    * The following would be a good method for controlling style loading
    */  
    
    if (is_category( $category ) ) {  
        wp_enqueue_script('myscript'); 
    }
    

    Also, WPA.SE has zounds of examples of wp_enqueue_script and questions