在 WooCommerce 中获取产品标签 URL

Get the Product tag URL in WooCommerce

我一直在使用产品标签作为我店里产品的品牌。我在网上找到的以下代码一直运行良好(直到最近更新了 woocommerce 和 WP),以便在我的 "brands" 页面上列出一个列表。它列出了每个特定类别字母下的所有品牌。

突然没有生成任何 URL,其他所有内容都按应有的方式填充。我真的找不到问题。我试图将 get_terms 修改为 get_terms(array('taxonomy'=>'product_tag')) 但它也不起作用。

这是我的代码:

$list = '';
$tags = get_terms( 'product_tag' );
$groups = array();
if( $tags && is_array( $tags ) ) {
    foreach( $tags as $tag ) {
        $first_letter = strtoupper( $tag->name[0] );
        $groups[ $first_letter ][] = $tag;
    }
    if( !empty( $groups ) ) {
        foreach( $groups as $letter => $tags ) {
            $list .= "\n\t" . '<div class="brand_category_container">';
            $list .= "\n\t" . '<h4 class="cateogry_letter">' . apply_filters( 'the_title', $letter ) . '</h2>';
            $list .= "\n\t" . '<ul>';
            foreach( $tags as $tag ) {
                $url = get_tag_link( $tag->term_id );
                $count = intval( $tag->count );
                $name = apply_filters( 'the_title', $tag->name );
                $list .= "\n\t\t" . '<li><a class="category_links" href="' . $url . '">' . $name . '</a></li>';
                }
            $list .= "\n\t" . '</ul></li>';
            $list .= "\n\t" . '</div>';
        }
    }
} else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';

在我的产品页面上,我用

列出了品牌
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
?>
<div class="single_productbrand">
    <?php echo $product->get_tags( ', ', '<span class="brand_as">' . _n( '', '', $tag_count, 'woocommerce' ) . ' ', '</span>' ); ?>
</div>

它能正常工作

我怎样才能取回网址?

您的代码中与 product tag terms links 相关的主要问题是这一行:

$url = get_tag_link( $tag->term_id );

It needs to be replaced with get_term_link() WordPress function that requires 2 arguments, the term object (or term ID) and the taxonomy.

您现在可能已经知道,产品标签是 自定义分类法 'product_tag'

同样在您的代码中,即使它有效,您最好使用不同的变量名称,因为您在 2 个具有不同值的不同部分使用 $tags 变量名称。

因此您重新访问的功能代码将是:

$taxonomy = 'product_tag';
$tags = get_terms( $taxonomy );
if( $tags && is_array( $tags ) ) {
    foreach( $tags as $tag ) {
        $first_letter = strtoupper( $tag->name[0] );
        $groups[ $first_letter ][] = $tag;
    }
    if( !empty( $groups ) ) {
        $list = '';
        foreach( $groups as $letter => $letter_tags ) {
            $list .= "\n\t" . '<div class="brand_category_container">';
            $list .= "\n\t" . '<h4 class="cateogry_letter">' . apply_filters( 'the_title', $letter ) . '</h2>';
            $list .= "\n\t" . '<ul>';
            foreach( $letter_tags as $tag ) {
                $term_link = get_term_link( $tag->term_id, $taxonomy );
                $count = intval( $tag->count );
                $name = apply_filters( 'the_title', $tag->name );
                $list .= "\n\t\t" . '<li><a class="category_links" href="' . $term_link . '">' . $name . '</a></li>';
            }
            $list .= "\n\t" . '</ul></li>';
            $list .= "\n\t" . '</div>';
        }
    }
}
else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';

// Output
echo $list;

这次 Urls 正确指向正确的路径...

感谢您的回答LoicTheAztec,它帮助我解决了问题,但是当我用 intval 转换 id 时。

注意:让我告诉你,当我将 WordPress 更新到 4.9 版本时发生了这种情况,否则 get_tag_link 对我来说工作得很好。 再次感谢