【问题标题】:Custom dropdown list of terms as links (woocommerce, wordpress)作为链接的术语的自定义下拉列表(woocommerce,wordpress)
【发布时间】:2021-08-01 17:03:48
【问题描述】:

我想添加一个属性的自定义下拉列表(在本例中是品牌),其中的选项作为链接指向属性页面。

我搞定了

add_filter('woocommerce_before_shop_loop','wc_reg_for_menus', 1, 2);
function wc_reg_for_menus() {
    $terms = get_terms( 'pa_marke' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        echo '<select>';
        foreach ( $terms as $term ) {
            echo '<option value="'.$term->name.'">'.$term->name.'</option>';
        }
        echo '</select>';
    }
}

我认为我需要添加这部分

get_term_link( WP_Term|int|string $term, string $taxonomy = '' )

谢谢! 费利克斯

【问题讨论】:

    标签: php wordpress woocommerce dropdown


    【解决方案1】:

    您的代码非常适合获取特定分类(属性)的值。
    我只更改了 get_terms 函数。

    我还添加了一些数据属性来获取每个术语(选项)的分类和 slug。

    add_filter( 'woocommerce_before_shop_loop','wc_reg_for_menus' );
    function wc_reg_for_menus() {
    
        $terms = get_terms( array(
            'taxonomy' => 'pa_marke',
            'hide_empty' => false,
        ));
    
        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
            echo '<select id="shop_pa_marke">';
            foreach ( $terms as $term ) {
                echo '<option value="' . $term->name . '" data-taxonomy="' . $term->taxonomy . '" data-slug="' . $term->slug . '">' . $term->name . '</option>';
            }
            echo '</select>';
        }
    }
    

    然后您将需要使用 jQuery 脚本根据选择的属性选项重定向用户。

    add_action( 'wp_footer', 'redirect_after_select_option' );
    function redirect_after_select_option() {
        ?>
        <script type="text/javascript">
            jQuery( function($){
                $('#shop_pa_marke').change(function(){
    
                    const url = window.location.href;
                    const taxonomy = $(this).children("option:selected").data('taxonomy').replace('pa_','filter_');
                    const slug = $(this).children("option:selected").data('slug');
    
                    let urlParams = new URLSearchParams(window.location.search);
                    urlParams.set(taxonomy, slug);
                    window.location.replace( '?'+urlParams.toString() );
    
                });
            });
        </script>
        <?php
    }
    

    代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

    【讨论】:

      【解决方案2】:

      谢谢!

      刚才我找到了一个没有 jQuery 的解决方案,效果也很好:

      
      
      add_filter('woocommerce_before_shop_loop','wc_reg_for_menus', 1, 2);
      function wc_reg_for_menus() {
           $terms = get_terms( 'pa_marke' );
       
      if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
       
      echo '<select onchange="location = this.value;">';
       
      foreach ( $terms as $term ) {
       
      echo '<option value="'.get_term_link( $term ).'">'.$term->name.'</option>';
      }
       
      echo '</select>';
       
      }
      }
      

      只有在新页面加载后,菜单的第一个选项才会再次被选中,但这对我来说没问题,因为以后不会在那里显示。

      【讨论】:

        猜你喜欢
        • 2019-01-16
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2014-12-21
        • 2012-07-20
        • 1970-01-01
        • 2015-04-07
        相关资源
        最近更新 更多