Make tag show as dropdown in woocommerce

前端 未结 2 1265
囚心锁ツ
囚心锁ツ 2021-01-27 06:58

Hi this is the code to the wordpress shown tags:


                    

        
相关标签:
2条回答
  • 2021-01-27 07:38
    <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$1 onchange='return this.form.submit()'>", $select);
            echo $select;
            ?>
            <noscript><div><input type="submit" value="View" /></div></noscript>
        </div>
    </form>
    

    Would you please try above code?

    0 讨论(0)
  • 2021-01-27 08:02

    Paste this code to functions.php end echo anywhere you want. You can also make shortcode with: add_shortcode( 'NAME TO DISPLAY', 'displayLocationDropdown' )'. Please also look at comments.

    <?php
    
    function displayLocationDropdown() {
    
      $html = '';
        $html .= '<form class="location-select" method="post">';
        $html .= '<select id="location-selector" name="location" class="location">';
    
        $tag = wp_tag_cloud( array(
            'format' => 'array',
            'taxonomy' => 'product_tag' 
        ) );
        $page_path = $_SERVER["REQUEST_URI"];
        $page_url = site_url() . $page_path;
        $url_parts = parse_url($page_url);
        $constructed_url = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['path'])?$url_parts['path']:'');
    
        $html .= '<option value="#">--Select Location--</option>';
    
        foreach($tag as $tagkey => $tagvalue)
        {
            $cleanedup = strip_tags($tagvalue);
            $tag_info = get_term_by('name', $cleanedup, 'product_tag', 'ARRAY_A');
            $tag_slug = $tag_info['slug'];
            if(isset($_GET['product_tag'])) { /// I am not sure if it is necessery
                $value_url = $constructed_url . '?product_tag=' . $tag_slug;
            } else {
                $value_url = $page_url . '?product_tag=' . $tag_slug;
            }
            /// (this prevent duplicate in URL in my case) $value_url = site_url() . '?product_tag=' . $tag_slug;
            $html .= '<option value="' . $value_url . '">' . $cleanedup . '</option>';
        }
        $html .= '<option value="' . $constructed_url . '">View All Locations</option>';
    
        $html .= '</select>';
        $html .= '</form>';
    
        echo $html;
    }
    
    add_action('woocommerce_before_shop_loop', 'displayLocationDropdown', 40);
    
    add_action('wp_head', 'addDisplayLocationScript');
    function addDisplayLocationScript() {
        $script = '';
        $script .= '<script type="text/javascript">';
        $script .= 'jQuery(document).ready(function() {';
        $script .= '    jQuery("#location-selector").change(function() {';
        $script .= '        location = jQuery("#location-selector option:selected").val();';
        $script .= '    });';
        $script .= '});';
        $script .= '</script>';
    
        echo $script;
    }
    
    0 讨论(0)
提交回复
热议问题