为 WooCommerce 分类法制作下拉菜单,例如产品标签
Make a dropdown for a WooCommerce taxonomy like product tags
我发现这个 post () 可以实现此功能的一部分,但输出不正确。使用此代码段时,结果仅输出 post 标签而不是产品标签。
<label><?php _e('Tags'); ?></label>
<form action="<?php bloginfo('url'); ?>/" method="get">
<div>
<?php
$args = array(
'taxonomy' => 'product_tag', // Taxonomy to return. Valid values are 'category', 'post_tag' or any registered taxonomy.
'show_option_none' => 'Select tag',
'show_count' => 1,
'orderby' => 'name',
'value_field' => 'slug',
'echo' => 0
);
$select = wp_dropdown_categories( $args );
$select = preg_replace("#<select([^>]*)>#", "<select onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div>
</form>
当前代码段产生此输出:https://www.website.com/?cat=books
结果应该是:https://www.website.com/product-tag/books/
您可以为任何 WooCommerce 产品分类法构建一个自定义下拉列表作为可以在任何地方使用的简码,这样:
add_shortcode( 'product_tax_dropdown', 'wc_product_taxonomy_dropdown' );
function wc_product_taxonomy_dropdown( $atts ) {
// Attributes
$atts = shortcode_atts( array(
'hide_empty' => '1', // or '0'
'show_count' => '1', // or '0'
'orderby' => 'name', // or 'order'
'taxonomy' => 'product_tag',
), $atts, 'product_tax_dropdown' );
global $wp_query;
$taxonomy = $atts['taxonomy'];
$taxonomy_name = get_taxonomy( $taxonomy )->labels->singular_name;
ob_start();
wp_dropdown_categories( array(
'hide_empty' => $atts['hide_empty'],
'show_count' => $atts['show_count'],
'orderby' => $atts['orderby'],
'selected' => isset( $wp_query->query_vars[$taxonomy] ) ? $wp_query->query_vars[$taxonomy] : '',
'show_option_none' => sprintf( __( 'Select a %s', 'woocommerce' ), $taxonomy_name ),
'option_none_value' => '',
'value_field' => 'slug',
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'class' => 'dropdown_'.$taxonomy,
) );
?>
<script type='text/javascript'>
jQuery(function($){
var select = '.dropdown_product_tag',
taxonomy = '<?php echo $taxonomy; ?>';
function onProductTaxChange() {
if ( $(select).val() !=='' ) {
location.href = '<?php echo esc_url( home_url() ); ?>/?'+taxonomy+'='+$(select).val();
}
}
$(select).change( onProductTaxChange );
});
</script>
<?php
return ob_get_clean();
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
用法
1) 作为普通短代码(在 Wordpress 文本编辑器或小部件中):
[product_tax_dropdown];
2) 在 PHP 模板、页面和函数简码中:
echo do_shortcode('[product_tax_dropdown]');
我发现这个 post (
<label><?php _e('Tags'); ?></label>
<form action="<?php bloginfo('url'); ?>/" method="get">
<div>
<?php
$args = array(
'taxonomy' => 'product_tag', // Taxonomy to return. Valid values are 'category', 'post_tag' or any registered taxonomy.
'show_option_none' => 'Select tag',
'show_count' => 1,
'orderby' => 'name',
'value_field' => 'slug',
'echo' => 0
);
$select = wp_dropdown_categories( $args );
$select = preg_replace("#<select([^>]*)>#", "<select onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div>
</form>
当前代码段产生此输出:https://www.website.com/?cat=books 结果应该是:https://www.website.com/product-tag/books/
您可以为任何 WooCommerce 产品分类法构建一个自定义下拉列表作为可以在任何地方使用的简码,这样:
add_shortcode( 'product_tax_dropdown', 'wc_product_taxonomy_dropdown' );
function wc_product_taxonomy_dropdown( $atts ) {
// Attributes
$atts = shortcode_atts( array(
'hide_empty' => '1', // or '0'
'show_count' => '1', // or '0'
'orderby' => 'name', // or 'order'
'taxonomy' => 'product_tag',
), $atts, 'product_tax_dropdown' );
global $wp_query;
$taxonomy = $atts['taxonomy'];
$taxonomy_name = get_taxonomy( $taxonomy )->labels->singular_name;
ob_start();
wp_dropdown_categories( array(
'hide_empty' => $atts['hide_empty'],
'show_count' => $atts['show_count'],
'orderby' => $atts['orderby'],
'selected' => isset( $wp_query->query_vars[$taxonomy] ) ? $wp_query->query_vars[$taxonomy] : '',
'show_option_none' => sprintf( __( 'Select a %s', 'woocommerce' ), $taxonomy_name ),
'option_none_value' => '',
'value_field' => 'slug',
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'class' => 'dropdown_'.$taxonomy,
) );
?>
<script type='text/javascript'>
jQuery(function($){
var select = '.dropdown_product_tag',
taxonomy = '<?php echo $taxonomy; ?>';
function onProductTaxChange() {
if ( $(select).val() !=='' ) {
location.href = '<?php echo esc_url( home_url() ); ?>/?'+taxonomy+'='+$(select).val();
}
}
$(select).change( onProductTaxChange );
});
</script>
<?php
return ob_get_clean();
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
用法
1) 作为普通短代码(在 Wordpress 文本编辑器或小部件中):
[product_tax_dropdown];
2) 在 PHP 模板、页面和函数简码中:
echo do_shortcode('[product_tax_dropdown]');