在你的代码中有:
- 代码中的一些错误,例如错误'显示计数'那是
'show_count'
(没有s
) … Now 隐藏柜台已启用并正常运行。
- 缺少参数“hide_empty”显示空类别
在此短代码中,您可以更改以下可选参数:
-
hierarchical
默认情况下禁用(设置为“0”)
-
hide_empty
默认情况下禁用(设置为“0”)
-
show_count
那是现在默认禁用(设置为“0”)
-
depth
默认情况下禁用(设置为“0”)
-
orderby
默认设置为“订单”类别(也可以按名称:“名称”)
添加了自定义钩子woocommerce_product_categories_shortcode_dropdown_args
这将允许扩展定制……
这是新代码:
add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
// Attributes
$atts = shortcode_atts( array(
'hierarchical' => '0', // or '1'
'hide_empty' => '0', // or '1'
'show_count' => '0', // or '1'
'depth' => '0', // or Any integer number to define depth
'orderby' => 'order', // or 'name'
), $atts, 'product_categories_dropdown' );
ob_start();
wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_shortcode_dropdown_args', array(
'depth' => $atts['depth'],
'hierarchical' => $atts['hierarchical'],
'hide_empty' => $atts['hide_empty'],
'orderby' => $atts['orderby'],
'show_uncategorized' => 0,
'show_count' => $atts['show_count'],
) ) );
?>
<script type='text/javascript'>
jQuery(function($){
var product_cat_dropdown = $(".dropdown_product_cat");
function onProductCatChange() {
if ( product_cat_dropdown.val() !=='' ) {
location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
}
}
product_cat_dropdown.change( onProductCatChange );
});
</script>
<?php
return ob_get_clean();
}
代码位于活动子主题(或活动主题)的 function.php 文件中。
经过测试并有效。
1) 用法示例- 所有产品类别和子类别分层显示:
[product_categories_dropdown orderby='name' hierarchical='1']
在 php 代码中你可以这样使用它:
echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']");
或者插入html标签中:
<?php echo do_shortcode("[product_categories_dropdown orderby='name' hierarchical='1']"); ?>
2) 用法示例- 仅“主要母体”产品类别:
[product_categories_dropdown depth='1' hierarchical='1']
在 php 代码中你可以这样使用它:
echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']");
或者插入html标签中:
<?php echo do_shortcode("[product_categories_dropdown depth='1' hierarchical='1']"); ?>