【问题标题】:Getting the Product Attribute Term Permalink in WordPress / WooCommerce在 WordPress / WooCommerce 中获取产品属性术语永久链接
【发布时间】:2016-11-14 11:56:11
【问题描述】:

我正在使用 WooCommerce 和 WordPress 来建立古董照片商店。

我正在使用 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 中找不到任何文档,这似乎是分类法中的分类法,比标准的 WordPress 更进一步,但我假设这是一个相当标准的要求。任何指针表示赞赏。

【问题讨论】:

  • 属性不是分类中的分类,它只是自定义分类。
  • 啊,所以大概 get_the_terms 会起作用?会试一试并相应更新
  • 是的,大多数与术语相关的功能应该可以工作。

标签: wordpress woocommerce


【解决方案1】:

一旦你有了属性词(在本例中是摄影师的名字),你就可以使用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 不是一个漂亮的固定链接。已经很晚了,我不知道这是否与我的配置有关,或者究竟是什么,但这是一个开始。

【讨论】:

  • 我必须在属性编辑屏幕上启用存档。刷新了永久链接,现在我有了漂亮的网址。
  • 另外,我在get_term_link 附近使用了esc_url,但这只是我在吹毛求疵:)。再次感谢@helgatheviking 的宝贵答案!
  • 你说得对,应该是esc_url() 而不是esc_attr()。我会改变的。
【解决方案2】:

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

$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>';
}

但肯定可以做一些改进。

【讨论】:

  • 是的,这与我想出的非常相似。 get_the_terms()wc_product_terms() 产生非常相似的结果。
猜你喜欢
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 2021-05-22
相关资源
最近更新 更多