Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;

}

share|improve this question
    
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 '11 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 '11 at 17:31

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
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 '11 at 19:47
    
No problem! Glad i could help. –  chrisjlee Dec 12 '11 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 –  Terrell Anderson Aug 20 '12 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 '13 at 19:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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