Skip Navigation

[Resolved] Setting up conditional css depending on taxonomy

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to set up a conditional that tests which term from a specific taxonomy is applied to the current post in the loop. Based on that term, I would like to apply a specific CSS class to my content.

Solution: The has_term function can be used to test whether or not the current post has a specific term applied. You can use that function in a conditional to output different bits of markup depending on the term applied to the current post. This approach doesn't work well if multiple terms from the taxonomy can be associated with a single post. It is best when only a single term is applied to a single post.

[wpv-conditional if="( has_term('one', 'category', null) eq '1' )"]
<div class="category-one">
[/wpv-conditional]
   
   
[wpv-conditional if="( has_term('two', 'category', null) eq '1' )"]
<div class="category-two">
[/wpv-conditional]
   
   
   
[wpv-conditional if="( has_term('three', 'category', null) eq '1' )"]
<div class="category-three">
[/wpv-conditional]
  
[wpv-conditional if="( has_term('four', 'category', null) eq '1' )"]
<div class="category-four">
[/wpv-conditional]
   
   
[wpv-conditional if="( has_term('five', 'category', null) eq '1' )"]
<div class="category-five">
[/wpv-conditional]
   
   
   
[wpv-conditional if="( has_term('six', 'category', null) eq '1' )"]
<div class="category-six">
[/wpv-conditional]
 
[wpv-post-link]
[wpv-post-body view_template="None"]
 
</div>

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-custom-functions-in-conditions/

This support ticket is created 4 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by MorganM3180 4 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1492355

Tell us what you are trying to do?

I'm trying to style each taxonomy business type (retail, restaurants, lodging, health, services, etc)with its own css styles - in the archive, the taxonomy archives, and in the single business template. I want the name of the category to be a particular color, a text background block will be in the same particular color, etc.

Is there any documentation that you are following?
I was using a solved support request, "https://toolset.com/forums/topic/css-for-categories/", however, it wasn't clear to me how to wrap the field using the wpv-conditional statement, if there are 6 different terms that may be returned within the taxonomy field. It's a semantic question.

Is there a similar example that we can see?
I can show you what I want it to do via Adobe XD, but I don't have a site example.

What is the link to your site? hidden link - the site is in development, but the archive would be hidden link and an example of the content template would be hidden link

#1492629

Hello, the solution in the other ticket can be extended like so:

[wpv-conditional if="( has_term('one', 'category', null) eq '1' )"]
<div class="category-one">
[/wpv-conditional]
 
 
[wpv-conditional if="( has_term('two', 'category', null) eq '1' )"]
<div class="category-two">
[/wpv-conditional]
 
 
 
[wpv-conditional if="( has_term('three', 'category', null) eq '1' )"]
<div class="category-three">
[/wpv-conditional]

[wpv-conditional if="( has_term('four', 'category', null) eq '1' )"]
<div class="category-four">
[/wpv-conditional]
 
 
[wpv-conditional if="( has_term('five', 'category', null) eq '1' )"]
<div class="category-five">
[/wpv-conditional]
 
 
 
[wpv-conditional if="( has_term('six', 'category', null) eq '1' )"]
<div class="category-six">
[/wpv-conditional]

Is this what you're trying to accomplish? This approach only works if one term from the taxonomy will be assigned to each post.

#1492701

Yes, one term will be assigned to each post. Would you mind showing me a sample of how to wrap this around the field.

#1492707

I'm not sure I understand. The has_term function is used instead of a taxonomy shortcode if that's what you mean. If not, please show me which field you are referring to.

#1492769

I obviously am not understanding how to use this function. I've added "has_term" to the settings, do I then use the slugs in each conditional as in
[wpv-conditional if="( has_term('one', 'retail', null) eq '1' )"]
div class="retail">
[/wpv-conditional]

etc...

And, do I place this in the content template for the single post?

#1492783

The has_term function takes two slugs. The first slug is the term slug, and the second is the taxonomy slug. So if the taxonomy slug is "business-type" and the term slug is "retail", then you would use the conditional like this:

[wpv-conditional if="( has_term('retail', 'business-type', null) eq '1' )"]
<div class="retail">
[/wpv-conditional]

Then your retail content would go here, followed by a closing div.
</div>

And, do I place this in the content template for the single post?
Sure, you can place it in the template for single posts, or in an archive. Either is fine.

#1492863

This will work fine if I'm doing a single taxonomy archive, but what about if it's an archive with posts with all of the terms?

#1494181

This will work fine if I'm doing a single taxonomy archive, but what about if it's an archive with posts with all of the terms?
I'm not following. What is the problem, exactly? Here's an example:

[wpv-conditional if="( has_term('one', 'category', null) eq '1' )"]
<div class="category-one">
[/wpv-conditional]
  
  
[wpv-conditional if="( has_term('two', 'category', null) eq '1' )"]
<div class="category-two">
[/wpv-conditional]
  
  
  
[wpv-conditional if="( has_term('three', 'category', null) eq '1' )"]
<div class="category-three">
[/wpv-conditional]
 
[wpv-conditional if="( has_term('four', 'category', null) eq '1' )"]
<div class="category-four">
[/wpv-conditional]
  
  
[wpv-conditional if="( has_term('five', 'category', null) eq '1' )"]
<div class="category-five">
[/wpv-conditional]
  
  
  
[wpv-conditional if="( has_term('six', 'category', null) eq '1' )"]
<div class="category-six">
[/wpv-conditional]

[wpv-post-link]
[wpv-post-body view_template="None"]

</div>

This would wrap the post link (wpv-post-link) and post body (wpv-post-body) in a div containing a class for whatever term is assigned to the current post in the loop. It would work for any term, assuming as you said only one term is assigned to each post.

#1494277

Sorry I was being dense! Thanks so much for your patience. I got it. My issue is resolved now. Thank you!