WooCommerce:如何自定义产品分类小工具以显示指定分类?
Hey there! Let's tackle this problem step by step. Your current code works great for globally excluding specific categories from all product category widgets, but it doesn't let you configure each widget instance to show a unique set of categories. Here are two solid solutions to fix that:
Option 1: Use a Dedicated Widget Plugin
There's a WooCommerce-specific widget plugin built exactly for this use case. It adds a custom widget to your WordPress admin that lets you select specific product categories for each widget instance directly from the widget settings panel—no code required. This is the quickest way to get up and running if you prefer not to mess with custom code.
Option 2: Build a Custom Widget with Code
If you want full control, you can create a custom widget that extends WooCommerce's default product category widget, adding a setting to choose which categories to display per instance. Here's the code to do that:
/** * Custom WooCommerce Product Category Widget with Per-Instance Category Selection */ class Custom_WC_Product_Category_Widget extends WC_Widget_Product_Categories { /** * Constructor - Set up widget settings */ public function __construct() { $this->widget_description = __( 'A custom widget to display specific product categories per instance.', 'your-text-domain' ); $this->widget_id = 'custom_wc_product_categories'; $this->widget_name = __( 'Custom Product Categories', 'your-text-domain' ); // Add our custom "selected categories" setting to the widget options $this->settings = array_merge( parent::$settings, array( 'selected_categories' => array( 'type' => 'multiselect', 'std' => '', 'label' => __( 'Select Categories to Display', 'your-text-domain' ), 'options' => $this->get_product_category_options(), ), ) ); parent::__construct(); } /** * Fetch all product categories for the multiselect dropdown */ private function get_product_category_options() { $categories = get_terms( 'product_cat', array( 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', ) ); $options = array(); foreach ( $categories as $cat ) { $options[$cat->term_id] = $cat->name; } return $options; } /** * Override the widget output to use selected categories */ public function widget( $args, $instance ) { $selected_cats = ! empty( $instance['selected_categories'] ) ? $instance['selected_categories'] : array(); // If no categories are selected, fall back to default behavior if ( empty( $selected_cats ) ) { parent::widget( $args, $instance ); return; } // Modify category arguments to only include selected categories $cat_args = array( 'taxonomy' => 'product_cat', 'orderby' => 'name', 'show_count' => isset( $instance['show_count'] ) ? $instance['show_count'] : 0, 'hierarchical' => isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : 1, 'title_li' => '', 'include' => $selected_cats, // Key line: only show selected categories ); // Preserve the "show children only" setting if enabled if ( isset( $instance['show_children_only'] ) && $instance['show_children_only'] ) { $cat_args['child_of'] = get_queried_object_id(); } // Output the widget ob_start(); echo $args['before_widget']; if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; } echo '<ul class="product-categories">'; wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $cat_args ) ); echo '</ul>'; echo $args['after_widget']; echo ob_get_clean(); } } // Register our custom widget function register_custom_wc_category_widget() { register_widget( 'Custom_WC_Product_Category_Widget' ); } add_action( 'widgets_init', 'register_custom_wc_category_widget' );
How This Works:
- We create a new widget that inherits all features of WooCommerce's default product category widget.
- We add a multiselect dropdown to the widget settings where you can pick which categories to display for each instance.
- We override the widget's output logic to only fetch and display the selected categories.
Once you add this code to your theme's functions.php file or a custom plugin, you'll see a new "Custom Product Categories" widget in your WordPress admin. You can add multiple instances of this widget, each with its own set of selected categories.
内容的提问来源于stack exchange,提问作者user11242294

