【问题标题】:Make a dropdown for a WooCommerce taxonomy like product tags为 WooCommerce 分类法(如产品标签)制作下拉菜单
【发布时间】:2019-09-30 21:38:24
【问题描述】:

我发现这篇文章 (Make tag show as dropdown in woocommerce) 得到了这个函数的一部分,但输出不正确。使用这个sn-p时,结果只输出post tags而不是product tags。

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

当前的 sn-p 导致这个输出:https://www.website.com/?cat=books 结果应该是:https://www.website.com/product-tag/books/

【问题讨论】:

    标签: php jquery woocommerce shortcode custom-taxonomy


    【解决方案1】:

    您可以为任何 WooCommerce 产品分类构建一个自定义下拉列表,作为可以在任何地方使用的简码,这样:

    add_shortcode( 'product_tax_dropdown', 'wc_product_taxonomy_dropdown' );
    function wc_product_taxonomy_dropdown( $atts ) {
        // Attributes
        $atts = shortcode_atts( array(
            'hide_empty'   => '1', // or '0'
            'show_count'   => '1', // or '0'
            'orderby'      => 'name', // or 'order'
            'taxonomy'     => 'product_tag',
        ), $atts, 'product_tax_dropdown' );
    
        global $wp_query;
    
        $taxonomy      = $atts['taxonomy'];
        $taxonomy_name = get_taxonomy( $taxonomy )->labels->singular_name;
    
        ob_start();
    
        wp_dropdown_categories( array(
            'hide_empty' => $atts['hide_empty'],
            'show_count' => $atts['show_count'],
            'orderby'    => $atts['orderby'],
            'selected'           => isset( $wp_query->query_vars[$taxonomy] ) ? $wp_query->query_vars[$taxonomy] : '',
            'show_option_none'   => sprintf( __( 'Select a %s', 'woocommerce' ), $taxonomy_name ),
            'option_none_value'  => '',
            'value_field'        => 'slug',
            'taxonomy'   => $taxonomy,
            'name'       => $taxonomy,
            'class'      => 'dropdown_'.$taxonomy,
        ) );
    
        ?>
        <script type='text/javascript'>
            jQuery(function($){
                var select = '.dropdown_product_tag',
                    taxonomy = '<?php echo $taxonomy; ?>';
    
                function onProductTaxChange() {
                    if ( $(select).val() !=='' ) {
                        location.href = '<?php echo esc_url( home_url() ); ?>/?'+taxonomy+'='+$(select).val();
                    }
                }
                $(select).change( onProductTaxChange );
            });
        </script>
        <?php
    
        return ob_get_clean();
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试和工作。


    用法

    1) 作为普通的简码(在 Wordpress 文本编辑器或小部件中):

    [product_tax_dropdown];
    

    2) 在 PHP 模板、页面和函数短代码中:

    echo do_shortcode('[product_tax_dropdown]');
    

    【讨论】:

    • 您好,非常感谢您的回复!下拉菜单已创建,但任何选定标签都没有页面更改。我收到语法错误(意外标记),esprima.org/demo/validate.html
    • @joy 我再次对其进行了测试,它对我来说非常完美,没有任何问题。
    • shucks...我不确定那是什么问题。您是否注意到语法验证器引用了代码中的错误?我很感激你花时间来回应和测试——这对我不起作用。 :-(
    • 等等!有用。我正在使用文本小部件,但它在那里不起作用。但是当我将短代码移动到自定义 html 小部件中时......它现在可以工作了。这很奇怪,因为短代码也可以使用文本小部件来使用。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2014-10-26
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多