Woocommerce different widgets for different categories

时间:2017-06-15 09:47:18

标签: wordpress woocommerce hook-woocommerce

What I'm trying to accomplish sounds pretty simple: Use different widgets in a specific widget area depending on the category of products. To be more specific, I want to be able to change the woocommerce filter options, for example a PC category page should have the filter options HDD space and CPU type, whereas a computer-case category should have the filter options height and width of the case.

I guess something like this as depicted in https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/ would do the different category work:

add_action( 'woocommerce_before_main_content', 'bbloomer_loop_cat_slug');

function bbloomer_loop_cat_slug() {

if ( is_product_category( 'books' ) ) {
echo 'This will show on the Books Cat page';
} elseif ( is_product_category( 'chairs' ) ) {
echo 'This will show on the Chairs Cat page';
}

}

However, I am not sure on how to add or remove widgets in a specific widget area, the examples as seen on this site: https://wordpress.stackexchange.com/questions/26557/programmatically-add-widgets-to-sidebars seem like they would work but there might be an easier way since the answers are almost 3 years old. Any ideas?

1 个答案:

答案 0 :(得分:0)

So while it is possible to do it programmatically, Jetpack supplies you with a widget visibility option which is far easier to use and better maintained.