最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to filter widget(s) based on a specific category landing page

programmeradmin3浏览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?

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?

Share Improve this question edited Mar 17, 2020 at 21:05 klewis asked Mar 17, 2020 at 20:58 klewisklewis 8991 gold badge14 silver badges30 bronze badges 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 Commented Mar 17, 2020 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 Commented Mar 17, 2020 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 Commented Mar 17, 2020 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 Commented Mar 17, 2020 at 21:19
  • have you considered to use a plugin: search wordpress/plugins/search/widget+logic or wordpress/plugins/search/conditional+widget and jetpack also has a section for that... – Michael Commented Mar 17, 2020 at 21:43
Add a comment  | 

1 Answer 1

Reset to default 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/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/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.

发布评论

评论列表(0)

  1. 暂无评论