【问题标题】:Get the Product tag URL in WooCommerce在 WooCommerce 中获取产品标签 URL
【发布时间】:2017-11-20 21:33:42
【问题描述】:

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

突然没有生成任何 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>

它可以正常工作

如何取回网址?

【问题讨论】:

  • var_dump($tags); 的输出是什么?

标签: php wordpress woocommerce tags taxonomy


【解决方案1】:

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

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

需要替换为get_term_link() WordPress 函数,该函数需要 2 个参数,即术语对象(或术语 ID)和分类。

您现在可能已经知道,产品标签是一种自定义分类法 '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;

这次 URL 正确指向了正确的路径...

【讨论】:

  • 非常感谢您在指出 get_term_link() 以及其他代码改进方面的帮助。它现在可以正常工作了。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 2015-10-07
    • 2015-11-01
    • 2016-12-26
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2019-03-08
    相关资源
    最近更新 更多