在 WooCommerce 中为 wp_dropdown_categories

我正在为我的 WooCommerce 网站创建一个高级搜索表单,并用于wp_dropdown_categories()显示 2 个 WooCommerce 产品类别下拉列表(过滤器)。


这是我的代码


add_shortcode( 'new_filter_search_shortcode', 'new_filter_search' );

function new_filter_search() { ?>

<form name="myform" method="GET" action="<?php echo esc_url(home_url('/')); ?>">

<?php if (class_exists('WooCommerce')) : ?>

<?php 

    if(isset($_REQUEST['product_cat']) && !empty($_REQUEST['product_cat'])) {

        $optsetlect=$_REQUEST['product_cat'];

    }

    else {

        $optsetlect=0;  

    }


    $args = array(

        'show_option_all' => esc_html__( 'Make / Model', 'woocommerce' ),

        'orderby' => 'name',

        'child_of' => 142,

        'hierarchical' => 1,

        'class' => 'cat',

        'echo' => 1,

        'depth' => 2,

        'show_count' => 1,

        'value_field' => 'name',

        'selected' => $optsetlect

    );

    $args['taxonomy'] = 'product_cat';

    $args['name'] = 'model';              

    $args['class'] = 'cate-dropdown hidden-xs';

    wp_dropdown_categories($args);  

    

    $args = array(

        'show_option_all' => esc_html__( 'Year', 'woocommerce' ),

        'orderby' => 'name',

        'child_of' => 69,

        'hierarchical' => 1,

        'class' => 'cat',

        'echo' => 1,

        'depth' => 2,

        'show_count' => 0,

        'value_field' => 'slug',

        'selected' => $optsetlect

    );

    $args['taxonomy'] = 'product_cat';

    $args['name'] = 'year';              

    $args['class'] = 'cate-dropdown hidden-xs';

    wp_dropdown_categories($args);

    

?>

<?php endif; ?>

        


<button type="submit" title="<?php esc_attr_e('Search', 'woocommerce'); ?>" class="search-btn-bg"><span><?php esc_attr_e('Search','woocommerce');?></span></button>

</form>

<?php }

https://img2.mukewang.com/64e081130001a74304980329.jpg

https://img2.mukewang.com/64e0811d0001bce205351033.jpg

我想在我的下拉搜索中启用 select2,请帮忙。



莫回无
浏览 84回答 1
1回答

开心每一天1111

我重新审视了您的代码,例如在短代码函数中,内容应始终返回而不是回显。我已将短代码重命名为更好、更短的内容……并为两个下拉菜单启用了 select2。功能代码:add_shortcode( 'new_search', 'new_filter_search_shortcode' );function new_filter_search_shortcode( $atts ) {&nbsp; &nbsp; extract( shortcode_atts( array(&nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy'&nbsp; => 'product_cat', // Product category taxonomy (by default)&nbsp; &nbsp; ), $atts ) );&nbsp; &nbsp; ob_start(); // Start buffering&nbsp; &nbsp; ?>&nbsp; &nbsp; <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />&nbsp; &nbsp; <form name="myform" method="GET" action="<?php echo esc_url(home_url('/')); ?>">&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp;if (class_exists('WooCommerce') ) {&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_REQUEST[$taxonomy]) && ! empty($_REQUEST[$taxonomy])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $optsetlect = esc_attr($_REQUEST[$taxonomy]);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $optsetlect = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $class = 'cate-dropdown hidden-xs';&nbsp; &nbsp; &nbsp; &nbsp; wp_dropdown_categories( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'show_option_all'&nbsp; &nbsp;=> esc_html__( 'Make / Model', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'orderby'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'child_of'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 142,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hierarchical'&nbsp; &nbsp; &nbsp; => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'echo'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'depth'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'show_count'&nbsp; &nbsp; &nbsp; &nbsp; => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value_field'&nbsp; &nbsp; &nbsp; &nbsp;=> 'name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'selected'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => $optsetlect,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => $taxonomy,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'model',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> $class,&nbsp; &nbsp; &nbsp; &nbsp; ) );&nbsp; &nbsp; &nbsp; &nbsp; wp_dropdown_categories( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'show_option_all'&nbsp; &nbsp;=> esc_html__( 'Year', 'woocommerce' ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'orderby'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'child_of'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 69,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hierarchical'&nbsp; &nbsp; &nbsp; => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'echo'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'depth'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'show_count'&nbsp; &nbsp; &nbsp; &nbsp; => 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value_field'&nbsp; &nbsp; &nbsp; &nbsp;=> 'slug',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'selected'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => $optsetlect,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => $taxonomy,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'year',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> $class,&nbsp; &nbsp; &nbsp; &nbsp; ) );&nbsp; &nbsp; }&nbsp; &nbsp; $search_text = esc_html__('Search', 'woocommerce');&nbsp; &nbsp; ?>&nbsp; &nbsp; <button type="submit" title="<?php echo $search_text; ?>" class="search-btn-bg"><span><?php echo $search_text;?></span></button>&nbsp; &nbsp; </form>&nbsp; &nbsp; <?php&nbsp; &nbsp; // Enable select2 for both dropdowns&nbsp; &nbsp; if (class_exists('WooCommerce') ) {&nbsp; &nbsp; ?>&nbsp; &nbsp; <script>&nbsp; &nbsp; jQuery(function($){&nbsp; &nbsp; &nbsp; &nbsp; $('select#model').select2();&nbsp; &nbsp; &nbsp; &nbsp; $('select#year').select2();&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php&nbsp; &nbsp; wp_enqueue_script( 'select2' );&nbsp; &nbsp; }&nbsp; &nbsp; return ob_get_clean(); // return buffered content}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。用法: [new_search]或echo do_shortcode('[new_search]'); (在 PHP 代码中) .
打开App,查看更多内容
随时随地看视频慕课网APP