1

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.

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;

}

2
  • What kind of pages are you trying to apply this to? Terms don't exist for archives / pages, they exist(or relate to) posts(or post types), a set of posts may have varying terms associated with them, is there a particular kind of archive or view you want to apply this logic to? Please add as much detail as you can to the question, and break it down as if you're explaining it to a novice, it will make the question easier to follow for anyone reading.,.
    – t31os
    Dec 12, 2011 at 16:35
  • I'm sorry, I was afraid I was already rambling on too much in my post. I'll edit it with more information. 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...
    – Kathy
    Dec 12, 2011 at 17:31

1 Answer 1

2

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

4
  • Thank you very much! I actually found a function I was able to tweak to work with Justin's original code in the referenced article: is_tax! But I'm definitely looking into everything you noted to expand my understanding. I'm late to the WP game but I'm trying to learn!
    – Kathy
    Dec 12, 2011 at 19:47
  • No problem! Glad i could help.
    – chrisjlee
    Dec 12, 2011 at 21:31
  • Hey Kathy do u mind sharing the code that you tweaked to work cause i am also looking for something like this Aug 20, 2012 at 20:29
  • Hi Terrell, I actually listed my fix in the original post. Look for when I start to say "I found a solution..."
    – Kathy
    Feb 8, 2013 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.