[英]Display category name next to product name on single product page
I'm trying to add the category name next to the product name on my woocommerce page.我正在尝试在我的 woocommerce 页面上的产品名称旁边添加类别名称。 I would like to displays it because of SEO reasons, so that Google knows that this product belongs to this category.
因为SEO的原因我想把它展示出来,让谷歌知道这个产品属于这个类别。
My code looks like this at the moment我的代码目前看起来像这样
the_title( '<h1 class="product_title entry-title">', '</h1> <?php echo wc_get_product_category_list($product->get_id()) ?>' );
I tried to get it done by adding我试图通过添加来完成它
<?php echo wc_get_product_category_list($product->get_id()) ?>
but there is no category name and dev consolse etc also shows no errors.但是没有类别名称,开发控制台等也没有显示错误。
Instead try to replace your code with the following:而是尝试使用以下内容替换您的代码:
the_title( '<h1 class="product_title entry-title">', '</h1>' );
$terms = wp_get_post_terms( get_the_id(), 'product_cat' );
$term = reset($terms);
echo $term->name;
It should work.它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.