【问题标题】:Display Woocommerce Related Products By Custom Taxonomy按自定义分类法显示 Woocommerce 相关产品
【发布时间】:2021-07-30 23:01:06
【问题描述】:

我想根据自定义分类显示相关产品。有人知道有没有办法根据自定义分类强制显示相关产品。

【问题讨论】:

    标签: wordpress woocommerce custom-taxonomy related-products


    【解决方案1】:
    add_filter( 'woocommerce_output_related_products_args', 'prfx_change_related_products_count' );
    function prfx_change_related_products_count( $args ) {
    
    // Current post as global
    global $post;
    $terms = get_the_terms( $post->ID, 'your_custom_taxonomy' );
    
    // Empty array
    $ids = array();
    
    foreach ( $terms as $term ) {
        array_push($ids, $term->term_id);
    }
    
     $args['tax_query'] = array(
      array(
        'taxonomy' => 'your_custom_taxonomy',
        'terms' => $ids,
        'field' => 'term_id',
        'include_children' => true,
        'operator' => 'IN'
      )
    );
    
     return $args;
    }
    

    【讨论】:

    • 您好兄弟,将这段代码添加到 function.php 后,出现“此网站出现严重错误。”错误。
    • @liaz Ahmed 你能分享截图或错误代码以便理解吗?
    • 它不见了;对于 $args['tax_query'] 。
    猜你喜欢
    • 2019-06-15
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2019-11-12
    • 2018-11-24
    相关资源
    最近更新 更多