WooCommerce:显示类别名称

问题描述 投票:7回答:4

有没有办法在WooCommerce archive-product.php页面上显示产品类别名称。我的产品类别名称是“Bracelets”,我希望它在页面上显示为标题。我目前正在使用wp_title()

<h1><?php wp_title(); ?></h1>

但是它将它打印到页面上,如下所示:

» Product Categories » Bracelets

我正在获取父页面标题和类别名称,它们之间有分隔符(上图)。有没有什么方法可以让标题打印出“手镯”?

对此有任何帮助表示赞赏。

提前致谢!

php wordpress woocommerce
4个回答
18
投票

5
投票

使用get_categories函数检索类别名称: -

您可以使用此代码显示产品类别名称 -

<?php global $post, $product;
    $categ = $product->get_categories();
    echo $categ; ?>

2
投票

目前,您可以显示产品类别this way(Wordpress 4.x):

<?php echo wc_get_product_category_list($product->get_id()) ?>

0
投票

我认为它会打印出痕迹,因为它挂了woocommerce_breadcrumb。

您可以在主题中覆盖模板Archive-product.php

<?php
    /**
     * woocommerce_before_main_content hook
     *
     */
    do_action( 'my_woocommerce_before_main_content' );
?>

并在functions.php中创建另一个动作,

add_action( 'my_woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
add_action( 'my_woocommerce_before_main_content', 'my_woocommerce_print_category_name', 20 );

在你的functions.php里面你也可以创建这样的函数:

function my_woocommerce_print_category_name() {
    //You can implements a function for get category name...    
}
© www.soinside.com 2019 - 2024. All rights reserved.