大家
我尝试在自定义帖子类型单个帖子上回显当前类别,但似乎我错过了一些重要的事情。由于蛞蝓没有回声。我尝试了不同的方法来解决这个问题,但没有办法让它发挥作用。
为了给您提供上下文,应该在锚链接上回显该 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>
由于这是关于 WooCommerce 产品,因此产品类别是自定义分类法,不适用于 WordPress 类别。
尝试以下操作:
if( 'product' == get_post_type() ) {
$taxonomy = 'product_cat'; // WooCommerce product category taxonomy
$terms = (array) wp_get_post_terms( get_the_ID(), $taxonomy );
if ( count($terms) > 0 ) {
$term = reset($terms); // Get the first WP_Term object from the array.
printf('<center><a href="%s" class="grve-btn grve-btn-small grve-square grve-bg-primary-1 grve-bg-hover-%s">%s</a></center>',
get_term_link( $term, $taxonomy ), $term->slug, $term->name );
}
}
应该可以。