[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?
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.
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.
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?
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?
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.
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.
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: