将标签显示为woocommerce中的下拉列表

时间:2016-09-19 08:20:02

标签: php wordpress wordpress-theming

嗨,这是wordpress显示标签的代码:

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

我想将其显示为下拉菜单,但我无法弄清楚:(有人可以帮助我吗

2 个答案:

答案 0 :(得分:2)

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

请您尝试上面的代码吗?

答案 1 :(得分:1)

将此代码粘贴到functions.php端回显您想要的任何位置。您还可以使用以下命令进行短代码:add_shortcode('NAME TO DISPLAY','displayLocationDropdown')'。 另请查看评论。

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