Make tag show as dropdown in woocommerce

十年热恋 提交于 2020-01-25 21:40:08

问题


Hi this is the code to the wordpress shown tags:

<label><?php _e('Tags', PLSH_THEME_DOMAIN); ?>:</label>
                    <?php
                    foreach($tags as $tag)
                    {
                        echo '<a href="' . plsh_assamble_url($shop_page_url, array('product_tag=' . $tag->slug), array('product_tag')) . '"';
                        if(plsh_get($_GET, 'product_tag') == $tag->slug) echo 'class="active"';
                        echo '>' . $tag->name . '</a>';
                    }
                    ?>

I want to make this shown as drop-down menu but I can not figure it out :( can someone help me pls


回答1:


<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?




回答2:


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;
}


来源:https://stackoverflow.com/questions/39568407/make-tag-show-as-dropdown-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!