Skip Navigation

[Resolved] Conditional Logic for Sidebar Widgets in Taxonomy Archives

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

Problem:

When I pasted the [wpv-archive-title] shortcode directly into the Custom HTML or Text Widget it works as expected. As far I can tell now, a simple conditional works as well when entered directly into the widget.

But when the same thing is saved as a content template in Views, and the content template shortcode [wpv-post-body view_template="my-conditional-widget"] is inserted into a widget, it will not output the archive title, nor will a conditional containing this title work. The same content template works fine with conditionals in sidebars on single posts.

I suppose this would be a workaround, but shouldn't it work with content template shortcodes as well?

Solution:

Q1) but shouldn't it work with content template shortcodes as well?
Please check our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-body
Outputs the content of the current post or renders a content template

It is not for rendering the content of current post, but shortcode [wpv-archive-title] is for rendering the tile of current archive page, so shortcode [wpv-archive-title] won't work as expected in the content template [wpv-archive-title] .
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-archive-title

Q2) I wonder if there are any reasons against pasting full, rather longish content template code into Custom HTML widgets? I suppose not?
See the reasons above in Q1), it is not recommended to put the shortcode [wpv-archive-title] into a content template.

Relevant Documentation:

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-archive-title

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-body

This support ticket is created 6 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 9 replies, has 3 voices.

Last updated by Luo Yang 6 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#601012

Hi guys,

I am building a content template showing a sidebar weather widget, according to a taxonomy (places). It uses conditional logic which modifies the location in the widget according to the taxonomy. If a post/archive belongs to a certain place, the widget should show the weather in that place.

Doing this for single posts is simple, and works fine, no problems.
But achieving the same for sidebars in archives seems tricky.

There is a shortcode [wpv-archive-title], which I used for conditionals in archives in the past. It works fine, but it was always in the content area. If I use it now in an archive sidebar, without a conditional, it won't even print the title. Nothing appears. If I paste it into the content (loop) area of the same archive, it prints the title.

It looks like the sidebar is completely unaware of the archive in which it appears. The title of the archive won't appear in the sidebar, so I suppose such a conditional can't work.

Is there any possibility to use a conditional in an archive sidebar?

I know there are plugins that create multiple sidebars based on conditions, but it would be an overkill for a simple widget and quite time-consuming to create a separate versions of the whole sidebar for each case. I also prefer to keep the number of plugins as low as possible, so I would be very grateful for your suggestions about solving it with Toolset's conditional logic.

Cheers
Tom

#601067

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2017-12-22 at 10.44.21.png

Hi Tom

I just checked this myself and the wpv-archive-title shortcode added to a custom HTML widget in the sidebar output the archive title as expected (and the wpv-post-title shortcode output nothing).

See screenshot.

I wonder if it is related to your theme? Can you try switching to twentyseventeen and testing?

#601192

Thank you Nigel. I switched from GeneratePress Pro to TwentySeventeen, deactivated non-essential plugins and it was the same.

But since you were able to output the title, I have experimented a bit. When I pasted the [wpv-archive-title] shortcode directly into the Custom HTML or Text Widget it works as expected. As far I can tell now, a simple conditional works as well when entered directly into the widget.

But when the same thing is saved as a content template in Views, and the content template shortcode [wpv-post-body view_template="my-conditional-widget"] is inserted into a widget, it will not output the archive title, nor will a conditional containing this title work. The same content template works fine with conditionals in sidebars on single posts.

I suppose this would be a workaround, but shouldn't it work with content template shortcodes as well?
I wonder if there are any reasons against pasting full, rather longish content template code into Custom HTML widgets? I suppose not?

Cheers
Tom

#601489

Dear Tom,

Q1) but shouldn't it work with content template shortcodes as well?
Please check our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-body
Outputs the content of the current post or renders a content template

It is not for rendering the content of current post, but shortcode [wpv-archive-title] is for rendering the tile of current archive page, so shortcode [wpv-archive-title] won't work as expected in the content template [wpv-archive-title] .
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-archive-title

Q2) I wonder if there are any reasons against pasting full, rather longish content template code into Custom HTML widgets? I suppose not?
See the reasons above in Q1), it is not recommended to put the shortcode [wpv-archive-title] into a content template.

#601498

Hello Luo Yang,

Thank you very much. I see. I have been frequently using Content Templates to insert repeating elements into posts. Now I understand why [wpv-archive-title] won't work in Content Templates.

I have one more question: what other options do we have to build conditionals in archives? If there are any other options.
I have always used [wpv-archive-title] and it works great. However, it has a significant limitation because it does not work for children term archives.

Let's say, I have the following taxonomy terms: "Asia -> Japan -> Tokyo" and "Asia -> Japan -> Osaka". Tokyo and Osaka archives are children of Japan. Is it possible to build a single conditional that would work like this:

If /taxonomy term archive is "Japan" or any of its children terms/ then /display something/.

With a [wpv-archive-title] conditional, it is necessary to list all possible children term archive titles, which can be hard to maintain for large taxonomies.

Using children in conditionals in posts is brilliantly simple with Toolset, but conditionals in archives seem tricky to me. The only way that I could find was with [wpv-archive-title] but then the condition must exactly match the title, so it doesn't work for children.

Kind regards,
Tom

#601507

You can setup the conditionals check with Views shortcode [wpv-conditional], use wordpress function is_tax() to check if it is archive page of specific term, for example:
1) Dashboard-> Toolset-> Settings-> Front-end Content
section "Functions inside conditional evaluations", add the function name: is_tax
2) In the widget, use below codes:

[wpv-conditional if="( is_tax('my-tax-slug', 'tokyo') eq 1 )"]
display something
[/wpv-conditional]

Please replace "my-tax-slug" with your custom taxonomy slug

More help:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/
https://codex.wordpress.org/Function_Reference/is_tax

#601545

Thank you very much! This is extremely useful!

As far I can see, this doesn't work for children terms, and only for the term listed in the conditional.
So, in:

if="( is_tax('my-tax-slug', 'japan') eq 1 )"]

the evaluation for children terms of "japan" will be "0".

Is it possible to make it include also children of the term appearing in the conditional?

An archive by default contains all children of the current term, so I suppose it would be possible to include children in the evaluation.

I checked the WordPress reference, but could not find a function for that.

Best regards,
Tom

#601665

It needs custom codes, for example, you can try this,
1) add below codes in your theme/functions.php:

function my_term_is_ancestor_of($taxonomy='', $term=''){
	global $wp_query;
	$res = 0;
	if(is_tax( $taxonomy )){
		$current_term_id = $wp_query->get_queried_object();
		$term_obj = get_term_by( 'slug', $term, $taxonomy);
		$term_id = $term_obj->term_id;
		
		if(is_tax( $taxonomy, $term )){
			$res = 1;
		}
		else if(term_is_ancestor_of( $term_id, $current_term_id, $taxonomy )){
			$res = 1;
		}
	}
	return $res;
}

2) Dashboard-> Toolset-> Settings-> Front-end Content
section "Functions inside conditional evaluations", add the function name: my_term_is_ancestor_of

3) In the widget, use below codes:

[wpv-conditional if="( my_term_is_ancestor_of('my-tax-slug', 'japan') eq 1 )"]
display something
[/wpv-conditional]
#601742

I works like a charm! Thank you very much, Luo. Fantastic support!

Kind regards,
Tom

#601878

You are welcome.