Php 使标记显示为woocommerce中的下拉列表

Php 使标记显示为woocommerce中的下拉列表,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,您好,这是wordpress显示标签的代码: <label><?php _e('Tags', PLSH_THEME_DOMAIN); ?>:</label> <?php foreach($tags as $tag) { echo '<a href="' . plsh_assamble

您好,这是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>';
                    }
                    ?>
我想将其显示为下拉菜单,但我想不出:(有人能帮我吗,请


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


将此代码粘贴到functions.php end echo任意位置。您还可以使用以下命令生成短代码: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;
}

将此代码粘贴到functions.php end echo的任意位置。您还可以使用以下命令生成快捷代码: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;
}

是的,这很有效,但它会给我页面标签,我想获得产品标签(woocommerce产品)你能帮我用与这些标签相同的方式获取woocommerce产品标签吗?你能在数组中更改你的
分类法
名称吗?请使用
产品标签
而不是
后标签
?purvik7373当我选择标签时,它不会重定向我所选的标签,它会让我代替我如何修复它是的,我看到了,但仍然是给我一个问题,它得到了这个url:它应该被取代?cat=它应该是?product_tag=所以结果应该是:再次抱歉打扰你,但我在这方面很糟糕:(@Argjent,我将'axonomy'改为'product_tag',它仍然将我重定向到/cat=…是的,这是有效的,但它给了我想要获得产品标签的页面标签(woocommerce产品)你能帮我用与这些标签相同的方式获取woocommerce产品标签吗?你能在数组中更改你的
分类法
名称吗?请使用
产品标签
而不是
后标签
?purvik7373当我选择标签时,它不会重定向我所选的标签,它会让我代替我如何修复它是的,我看到了,但仍然是给我一个问题,它得到这个url:它应该被替代?cat=它应该是?product_tag=所以结果应该是:再次抱歉打扰你,但我在这方面很糟糕:(@Argjent,我将'axonomy'更改为'product_tag',它仍然将我重定向到/cat=…@RobertColumbia,好的,你是对的,我编辑了我的帖子。@RobertColumbia,好的,你是对的,我编辑了我的帖子。