【问题标题】:Show Links to "Related Categories" in WooCommerce product在 WooCommerce 产品中显示“相关类别”的链接
【发布时间】:2018-09-26 10:14:53
【问题描述】:

我希望在我的产品信息中显示相关类别 - 类似于显示相关产品的方式,但显示指向类别存档的链接而不是单个产品。

例如,在洗涤槽产品上,相关类别将是“水龙头”和“镜子”,并会在产品页面上显示指向这些的链接。

这可能吗?我查看了很多扩展相关产品部分的插件,但它们只添加了显示不同类别产品的功能,而不是类别页面本身。

【问题讨论】:

    标签: php wordpress woocommerce product categories


    【解决方案1】:

    我设法解决了这个问题。我在 POD 中创建了一个关系字段以将产品链接到 product_cat,然后创建此代码以使用简码显示相关类别。

        function gr_related_categories(){
        //get Pods object for current post
        $pod = pods( 'product', get_the_id() );
    
    //get the value for the relationship field
    $related = $pod->field( 'related_categories' );
    
    if ( ! empty( $related ) ) {
        $rows.='<div style=" display: grid; grid-gap: 5px; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); grid-template-rows: repeat(3, 350px);">';
        foreach ( $related as $rel ) { 
            $id = $rel[ 'term_id' ];
            $term=get_term($id, 'product_cat');
    
            $name = $rel['name'];
            $slug = $rel['slug'];
            $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true );
            $image=wp_get_attachment_image($thumbnail_id, 'full');
            $url=get_term_link($term);
    
            $rows.= ' <div style="width:250px; height:350px; margin: 20px auto; border:solid 2px #5500B5; text-align:center; "><a href="'.$url.'" target="_blank">'.$image.'<br>'.$name.'</a></div>';
        } //end of foreach
        $rows.= '</div>';
    
    } //endif ! empty ( $related );
    return $rows;
    }
    
    add_shortcode('gr_related_categories', 'gr_related_categories');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      相关资源
      最近更新 更多