在 WordPress 上的自定义帖子类型单个帖子上回显当前类别的 slug

问题描述 投票:0回答:1

大家

我尝试在自定义帖子类型单个帖子上回显当前类别,但似乎我错过了一些重要的事情。由于蛞蝓没有回声。我尝试了不同的方法来解决这个问题,但没有任何效果。 为了给您提供上下文,该 slug 应该在锚链接上回显,这样我就可以在单个帖子上创建一个后退按钮以将用户引导至类别视图。

如果有人能在这方面帮助我,那就太好了。非常感谢。

 <?php
                        if('product' == get_post_type()) {
                            $terms = wp_get_post_terms($post->slug,'category');
                        ?>
                        
                        <center><a href="../../product-category/<?php echo $terms->slug ?>" class="grve-btn grve-btn-small grve-square grve-bg-primary-1 grve-bg-hover-black">Back</a></center>
php wordpress woocommerce product taxonomy-terms
1个回答
0
投票

函数 wp_get_post_terms 返回术语的数组。如果您想要第一类,

$terms = wp_get_post_terms($post_id,'category');
echo $terms[0]->slug;
© www.soinside.com 2019 - 2024. All rights reserved.