在WordPress/WooCommerce中获取产品属性术语Permalink

在WordPress/WooCommerce中获取产品属性术语Permalink,wordpress,woocommerce,Wordpress,Woocommerce,我正在用WooCommerce和WordPress建立一个古董照片商店 我正在使用WooCommerce产品属性功能来存储有关物品的信息 请参见此处的示例,摄影师属性在左侧的框中拉出 提取属性的代码如下所示: if($attribute['is_taxonomy']) { $values=wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names')); echo apply_

我正在用WooCommerce和WordPress建立一个古董照片商店

我正在使用WooCommerce产品属性功能来存储有关物品的信息

请参见此处的示例,摄影师属性在左侧的框中拉出

提取属性的代码如下所示:

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);
}
Array ( [0] => Photographer 1 )
$value如下所示:

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);
}
Array ( [0] => Photographer 1 )
问题是,我如何获得由WordPress和WooCommerce自动生成的摄影师永久链接:


我在WooCommerce中找不到任何关于这方面的文档,这似乎是比stabdard WordPress更进一步的分类法中的一个分类法,但我假设这是一个相当标准的要求。任何提示都值得欣赏。

这基本上满足了我的要求:

$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>';
}
$terms=获取术语($product->id,$attribute['name']);
foreach($terms作为$term){
回声';
}

但是可以肯定的是,这需要一些改进。

一旦你有了属性项(在本例中是摄影师的名字),你就可以用它来获取URL。由于
$product
id没有传递到
woocommerce\u属性
文件夹,因此我无法对其进行筛选,而是创建了
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 );

}
if($attribute['is_taxonomy'])){
$terms=wc_get_product_terms($product->id,$attribute['name'],数组('fields'=>'all'));
$html='';
$counter=1;
$total=计数($terms);
foreach($terms作为$term){
$html.=sprintf(“”,
esc_url(获取术语链接($term)),
esc_attr($term->name),
wptexturize($term->name)
);
如果($counter<$total){
$html.=',';
}
$counter++;
}
echo wpautop($html);
}

由于某些原因,URL不是一个很好的永久链接。很晚了,我不知道这是否与我的配置有关,或者具体是什么,但这只是一个开始

属性不是分类法中的分类法,它仅仅是一个自定义分类法。啊,那么,假设获取这些术语会起作用吗?是的,大多数与术语相关的函数应该可以工作。是的,这和我提出的非常相似
get\u the_terms()
wc\u product_terms()
产生了非常相似的结果。我必须在属性编辑屏幕上启用存档。刷新了永久链接,现在我有了漂亮的url。此外,我在
get\u term\u link
周围使用了
esc\u url
,但这只是我的吹毛求疵:)。再次感谢@helgatheviking为您提供的宝贵答案!没错,它应该是
esc\u url()
而不是
esc\u attr()
。我会改变的。