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 |1 Answer
Reset to default 1First 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744661241a4586475.html
is_category()
to vary content into widget. – Mikhail Commented Mar 17, 2020 at 21:05is_category
– Mikhail Commented Mar 17, 2020 at 21:15