Skip Navigation

[Resolved] Trying to display content conditionally based on category

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

Problem:
How to dispaly conditional output based on category/taxonomy/term.

Solution:
To dispaly conditional output based on category you can [wpv-conditional] shortcode.

You can use WordPress native function has_term() inside the [wpv-condtional] shortcode to check if post is assinged with specific category or not.

Please do not forget to register the has_term function at:
=> Toolset => Settings => Front-end Content tab => Functions inside conditional evaluations

You can find the proposed solution with the following reply.
=> https://toolset.com/forums/topic/trying-to-display-content-conditionally-based-on-category/#post-563046

Relevant Documentation:

- https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/

This support ticket is created 7 years, 3 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Kate 7 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#563025
Screenshot_Taxonomy Settings.JPG
Area I expect to see the term Residential.JPG
Screenshot_Experimenting with different if statements.JPG

I am trying to: Display content based on the category of the post.

Link to a page where the issue can be seen: hidden link (above the "View More" button on the bottom of the page.

I expected to see: The word 'Residential' appearing near the 'View MOre' button.

Instead, I got: nothing. None of my conditional statements are working 🙁

Can you point out where I've gone wrong?

Have attached screenshots. I've also referenced this article...with no luck obviously https://toolset.com/documentation/user-guides/conditional-html-output-in-views/displaying-taxonomies-conditionally/.

Thanks!

#563046

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - The Doc you are following is correct but you missed to register the function at:
Toolset -> Settings => Front-end Content tab => Functions inside conditional evaluations

Before using a function inside a conditional, you need to register it for security reasons. Even WordPress functions need to be registered. To do so, visit the Toolset -> Settings page and click the Front-end Content tab. There, simply add your function to the Functions inside conditional evaluations section.

See above instructions is already given under section "Using Custom Functions Or Class Methods" within blue background box.
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

Another issue was you pass the wrong argument to has_term() function. You need to pass your taxonomy slug as the second argument.

For example:

[wpv-conditional if="( has_term('Residential', 'category', null) eq '1' )"]
RESIDENTIAL 1
[/wpv-conditional]

Where:
- 'category' is your taxonomy category slug.

More info:
https://codex.wordpress.org/Function_Reference/has_term

#563918

Whoops - sorry for missing that.

Thanks so much Minesh, I really appreciate your help.