0

I apologize in advance if this question seems too basic. But I can't find any resources on Google that helps me to further my research.

My scenario:

  • I have a total of 3 unique category landing pages on my site.
  • I have a custom side bar that is loaded on every page.
  • Within this side bar I have a widget that lists articles based on a specific category I want to filter by.
  • From this same side-bar, in wp-admin->appearance->widgets, I have to add this widget 3 times to my side bar, so that each instance of this widget is filtering articles from each of my 3 category.

  • When the site loads, the side-bar will display 3 huge side-bar sections of links. I don't want that...I want my "category A" landing page to display category a articles only. When I go to my "category B" landing page, I want that to only display my category b articles, etc...

What I could do...

I could take the front-end approach and simply use JavaScript to modify the DOM once everything is loaded and hide 2 of the 3 sections I don't need for any given category landing page.

But what is the efficient WordPress way in PHP to filter the display of widgets by the category landing page they are on? Is there an advanced guide for me to look over to learn this technique?

5
  • If you use same sidebar on all category pages templates you have to move logic of detecting a category via conditional is_category() to vary content into widget.
    – Mikhail
    Mar 17 '20 at 21:05
  • thats exactly what I am looking to do, I want to (1) detect is_category(), (2) get name of widgets that are loaded, (3) if is_category is "A", then only show a_widget on this Category page. What would be a good resource for me to learn how to do this professionally?
    – klewis
    Mar 17 '20 at 21:10
  • I've meant you probably have to create your custom widget which will determine inside itself what content it should show based on is_category
    – Mikhail
    Mar 17 '20 at 21:15
  • I think we are on the same page, I just have to figure out several learning points that I could eventually combine together and build that solution. Thanks!
    – klewis
    Mar 17 '20 at 21:19
  • have you considered to use a plugin: search wordpress.org/plugins/search/widget+logic or wordpress.org/plugins/search/conditional+widget and jetpack also has a section for that...
    – Michael
    Mar 17 '20 at 21:43
1

First idea:

You can create custom widget which will inside it's displaying logic contain your necessary conditionals check to display only necessary or variable content, utilizing is_category() conditional. Check oficiall widget's docs and tutorial https://developer.wordpress.org/themes/functionality/widgets/

Another idea:

You can use filter sidebars_widgets inside which you'll remove unnecessary widgets out of being rendered based on your conditionals. See more info at https://developer.wordpress.org/reference/hooks/sidebars_widgets/

Approach with custom widget will be more universal as with it you'll be able to render it exactly the way you want. And filtering sidebars_widgets is more suitable for cases when you need to remove\hide certain widgets for example when user is logged in or not.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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