如果 WooCommerce 短代码没有返回产品,则显示一条消息

Display a message if there are no products returned for a WooCommerce shortcode

我有一个页面使用短代码 [products columns="4" category="free-prize-draws"] 来显示特定类别的产品。如果该类别中没有产品,则页面为空白。

如果产品短代码没有返回产品,是否可以让页面显示一条消息?如果没有特定搜索查询的搜索结果,则显示消息的方式相同。

您可以使用 do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes ); 动作挂钩。

所以你得到:

function action_woocommerce_shortcode_products_loop_no_results( $attributes ) {
    echo __( 'My custom message', 'woocommerce' );
}
add_action( 'woocommerce_shortcode_products_loop_no_results', 'action_woocommerce_shortcode_products_loop_no_results', 10, 1 );