如何在 loop-start.php 中显示产品类别标题?

时间:2021-07-14 15:24:09

标签: php wordpress woocommerce frontend wordpress-theming

我正在开发一个客户网站,该网站有一个商店页面,其中包含一系列产品类别。我想显示每个类别的标题,然后列出下面的所有产品。

我发现每个商店的列表都在模板文件中:loop-start.php,但我终其一生都无法弄清楚如何从 $product 中提取信息和类别标题...< /p>

我觉得这很简单,但我不确定我对 woocommerce 的模板结构有足够的了解以使其正常工作。有人能告诉我我做错了什么吗?

loop-start.php

<?php
/**
 * Product Loop Start
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/loop-start.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerce\Templates
 * @version     3.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<hr>
<?php

//add_action('woocommerce_shop_loop_item_title', 'the_loop_titlse', 10);

// global $wpdb;
// global $post, $product;
    
//     echo $product;

// $term_id = $_POST['term_id'];
// $tableTitle = $wpdb->get_results(
//     "SELECT slug FROM `wp_terms` WHERE term_id = '$term_id'");
// $page_title = $tableTitle[0]->slug;
// echo "<h3>page_title is ".$page_title." ...</h3>";
// echo  single_cat_title();
//echo get_queried_object()->term_id; 

    global $product;
    $product_cats = wp_get_post_terms($product->id, 'product_cat');
    $count = count($product_cats);

    //these echos return an empty array..
    echo $product->id;
    echo $count;
    echo $product_cats;

    // using some code I found to try to output the category title..
    foreach($product_cats as $key => $cat)
    {
        echo 
        '
        '.$cat->name.'';
        if($key < ($count-1))
        {
            echo 'aaa ';
        }
        else
        {
            echo 'bbb ';
        }
    }

?>
<h3>I want to show the category title here..</h3>
<ul class="products columns-<?php echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>">

0 个答案:

没有答案