首页 文章

获取WordPress / WooCommerce中的产品属性术语永久链接

提问于
浏览
0

我正在使用WooCommerce和WordPress Build 一个古董照片商店 .

我正在使用WooCommerce产品属性功能来存储有关项目摄影师的信息 .

在这里看一个例子,摄影师属性被拉出左边的方框http://bit.ly/1Dp999O

拉出属性的代码如下所示:

if($attribute['is_taxonomy']) {
  $values=wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names'));
  echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values);
}

$ values看起来像这样:

Array ( [0] => Photographer 1 )

问题是,如何进入由WordPress和WooCommerce自动生成的摄影师的永久链接:http://bit.ly/1JtwBna

我在WooCommerce中找不到任何相关文档,这似乎是分类中的分类法,比stabdard WordPress更进一步,但我认为这是一个相当标准的要求 . 任何指针赞赏 .

2 回答

  • 3

    获得属性术语(在本例中为摄影师的姓名)后,您可以使用get_term_link()获取URL . 因为 $product id没有传递给 woocommerce_attribute 文件夹,所以我无法对其进行过滤,而是创建了 product-attributes.php 模板的覆盖 . 并修改相关部分如下:

    if ( $attribute['is_taxonomy'] ) {
    
        $terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'all' ) );
    
        $html = '';
        $counter = 1;
        $total = count( $terms );
    
        foreach( $terms as $term ){ 
    
            $html .= sprintf( '<a href="%s" title="Permalink to %s">%s</a>', 
                esc_url( get_term_link( $term ) ), 
                esc_attr( $term->name ), 
                wptexturize( $term->name ) 
            );
    
            if( $counter < $total ){
                $html .= ', ';
            }
            $counter++;
    
        }
    
        echo wpautop( $html );
    
    }
    

    由于某种原因,URL不是一个非常固定的链接 . 现在已经很晚了,我无法判断这是否与我的配置有关或究竟是什么,但这是一个开始 .

  • 1

    这基本上符合我的要求:

    $terms = get_the_terms($product->id, $attribute['name']);
    foreach ($terms as $term){
        echo '<a href="'.get_term_link($term->slug, $term->taxonomy).'">'.$term->name.'</a>';
    }
    

    但肯定可以做一些改进 .

相关问题