【问题标题】:Modify tags URL for current product in Woocommerce在 Woocommerce 中修改当前产品的标签 URL
【发布时间】:2015-12-11 07:44:08
【问题描述】:

我发现这个可以让我显示当前产品页面的逗号分隔标签列表...

echo $product->get_tags( ', ', '<span>' . _n( '', '', $tag_count, 'woocommerce' ) . ' ', '</span>' );

...输出如下:

<span>
    <a href="http://example.com/product-tag/name-1/" rel="tag">Name 1</a>,
    <a href="http://example.com/product-tag/name-2/" rel="tag">Name 2</a>
</span>

但我真正想要的是将 URL 中的 /product-tag/ 部分更改为其他内容,如下所示:

<span>
    <a href="http://example.com/something-else/name-1/" rel="tag">Name 1</a>,
    <a href="http://example.com/something-else/name-2/" rel="tag">Name 2</a
</span>

任何帮助都会很棒。

【问题讨论】:

    标签: php wordpress tags woocommerce


    【解决方案1】:

    您需要先检索条款:

     $terms = wp_get_post_terms( $product->id, 'product_cat');
    

    然后循环遍历这些术语:

    foreach($terms as $term) {
       echo '<a href="'. get_term_link($term->term_id).'">'.$term->name.'</a>';
    }
    

    您必须根据自己的规则构建 URL,而不是 get_term_link。

    【讨论】:

    • 几乎完美的感谢!稍作修改,现在正是我需要的:$terms = wp_get_post_terms( $product-&gt;id, 'product_tag'); foreach($terms as $term) { echo '&lt;a href="'. $term-&gt;slug .'"&gt;'.$term-&gt;name.'&lt;/a&gt;'; }
    【解决方案2】:

    基于稍微修改来自@snowflake 的响应,这可行:

     $terms = wp_get_post_terms( $product->id, 'product_tag');
     foreach($terms as $term) {
       echo '<a href="'. $term->slug .'">'.$term->name.'</a>';
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2016-03-04
      相关资源
      最近更新 更多