第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 中為 wp_dropdown_categories

在 WooCommerce 中為 wp_dropdown_categories

PHP
莫回?zé)o 2023-08-19 16:44:30
我正在為我的 WooCommerce 網(wǎng)站創(chuàng)建一個高級搜索表單,并用于wp_dropdown_categories()顯示 2 個 WooCommerce 產(chǎn)品類別下拉列表(過濾器)。這是我的代碼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 }我想在我的下拉搜索中啟用 select2,請幫忙。
查看完整描述

1 回答

?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個贊

我重新審視了您的代碼,例如在短代碼函數(shù)中,內(nèi)容應(yīng)始終返回而不是回顯。


我已將短代碼重命名為更好、更短的內(nèi)容……并為兩個下拉菜單啟用了 select2。


功能代碼:


add_shortcode( 'new_search', 'new_filter_search_shortcode' );

function new_filter_search_shortcode( $atts ) {

    extract( shortcode_atts( array(

        'taxonomy'  => 'product_cat', // Product category taxonomy (by default)

    ), $atts ) );


    ob_start(); // Start buffering

    ?>

    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />

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

    <?php

     if (class_exists('WooCommerce') ) {

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

            $optsetlect = esc_attr($_REQUEST[$taxonomy]);

        } else {

            $optsetlect = 0;

        }


        $class = 'cate-dropdown hidden-xs';


        wp_dropdown_categories( array(

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

            'orderby'           => 'name',

            'child_of'          => 142,

            'hierarchical'      => 1,

            'echo'              => 1,

            'depth'             => 2,

            'show_count'        => 1,

            'value_field'       => 'name',

            'selected'          => $optsetlect,

            'taxonomy'          => $taxonomy,

            'name'              => 'model',

            'class'             => $class,

        ) );


        wp_dropdown_categories( array(

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

            'orderby'           => 'name',

            'child_of'          => 69,

            'hierarchical'      => 1,

            'echo'              => 1,

            'depth'             => 2,

            'show_count'        => 0,

            'value_field'       => 'slug',

            'selected'          => $optsetlect,

            'taxonomy'          => $taxonomy,

            'name'              => 'year',

            'class'             => $class,

        ) );

    }

    $search_text = esc_html__('Search', 'woocommerce');

    ?>

    <button type="submit" title="<?php echo $search_text; ?>" class="search-btn-bg"><span><?php echo $search_text;?></span></button>

    </form>

    <?php

    // Enable select2 for both dropdowns

    if (class_exists('WooCommerce') ) {

    ?>

    <script>

    jQuery(function($){

        $('select#model').select2();

        $('select#year').select2();

    });

    </script>

    <?php

    wp_enqueue_script( 'select2' );

    }

    return ob_get_clean(); // return buffered content

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經(jīng)過測試并有效。


用法: [new_search]或echo do_shortcode('[new_search]'); (在 PHP 代碼中) .


查看完整回答
反對 回復(fù) 2023-08-19
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號