我正在尝试为WordPress类别和标签分类法创建一个下拉简码。我似乎可以看到这些选项,但是选择后什么也没发生。
我已经设法修改了我用于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' => '0', // 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();
}
修改后的破碎代码段==>
add_shortcode( 'tax_dropdown', 'taxonomy_dropdown' );
function taxonomy_dropdown( $atts ) {
// Attributes
$atts = shortcode_atts( array(
'hide_empty' => '1', // or '0'
'show_count' => '0', // or '0'
'orderby' => 'name', // or 'order'
'taxonomy' => 'post_tag',
), $atts, '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', 'post' ), $taxonomy_name ),
'option_none_value' => '',
'value_field' => 'slug',
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'class' => 'dropdown_'.$taxonomy,
) );
?>
<script type='text/javascript'>
jQuery(function($){
var select = '.dropdown_tag',
taxonomy = '<?php echo $taxonomy; ?>';
function onTaxChange() {
if ( $(select).val() !=='' ) {
location.href = '<?php echo esc_url( home_url() ); ?>/?'+taxonomy+'='+$(select).val();
}
}
$(select).change( onTaxChange );
});
</script>
<?php
return ob_get_clean();
}
分类法显示在下拉菜单中,但是选择它们后什么也没有发生。
选择分类法后,应将用户定向到所选的存档页面:
类别示例:http://www.website.com/category/seleted-category 标记示例:http://www.website.com/tag/selected-tag
答案 0 :(得分:0)
替代解决方案
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' => '0', // or '0'
'orderby' => 'name', // or 'order'
'taxonomy' => 'product_tag',
), $atts, 'product_tax_dropdown');
ob_start();
?>
<select class="<?php echo $atts['taxonomy']; ?>" name="<?php echo $atts['taxonomy']; ?>">
<option><?php echo sprintf( __( 'Select a %s', 'woocommerce' ), $taxonomy_name ); ?> </option>
<?php
$terms = get_terms(array('taxonomy' => $atts['taxonomy'], 'hide_empty' => $atts['hide_empty'], 'show_count' => $atts['show_count'], 'orderby' => $atts['orderby']));
foreach ($terms as $key => $term) {
?>
<option value="<?php echo get_term_link($term->term_id, $atts['taxonomy']); ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
<script type='text/javascript'>
jQuery(function(){
var select = '.'<?php echo $atts['taxonomy']; ?>;
jQuery(select).on('change', function() {
if (jQuery(this).val() !== '') {
location.href = jQuery(this).val();
}
});
});
</script>
<?php
}