首页 文章

WooCommerce:类别产品循环奇怪的问题

提问于
浏览
1

我有一个php / wp脚本,理论上应该打印一个带有类别名称,描述及其所有产品的div . 代码如下所示:

<?php

$args2 = array( 'taxonomy'  =>   'product_cat', 'parent' => 9 );
$sub_cats = get_categories( $args2 );

foreach( $sub_cats as $sub_category ) { ?>

    <div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>">

        <h2 class="section-heading">
            <span class="line-behind-text"><?php echo $sub_category->name;?></span>
        </h2>

        <p class="section-text">
            OPIS: <?php echo category_description(); ?> //this part does not work too, not sure why
        </p>

        <h3 class="section-heading"><p class="line-behind-text">Dostępne zabiegi</p></h3>

        <table class="treatments-table table products">

            <tr class="table-heading">
                <th class="name" id="<?php echo $sub_category->term_id;?>">Usługa</th>
                <th>Czas trwania</th>
                <th>Cena</th>
                <th></th>
            </tr> <?php

            $name = $sub_category->name;
            $args = array( 'post_type' => 'product', 
                           "product_cat" => $sub_category->term_id //PROBLEM HERE
                    );

             $loop = new WP_Query( $args );

             if ( $loop->have_posts() ) {

                 while ( $loop->have_posts() ) : $loop->the_post();

                     $product = new WC_Product(get_the_ID()); ?>

                     <tr>
                         <td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td>
                         <td><?php the_excerpt(); ?></td>
                         <td><?php echo $product->price; ?>zł</td>
                         <td><button class="button-product materialbutton">Rezerwuj</button> </td>
                    </tr> <?php

                 endwhile;
             } 
             else {

                 echo __( 'No products found' );
             } ?>

             <h1>THE END</h1> <?php
} //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS

现在理论上它应该显示这样的div:

  • 类别a:1a . 类别说明1b . 分类表

  • 类别b:2a . 类别说明2b . 分类表

但相反,结果如下:
Outcome

因此,您看到它不仅没有正确布局页面(顺序是descrption1,description2,table1,table2,介意 <h1>THE END</h1> 的位置),它似乎也没有正确匹配类别的产品 . 当Id放入数组时会发生相同的结果

“product_cat”=> 14 //经过验证的类别ID,包含帖子

我在wp方面很有经验,但对于woocommerce来说却相当新鲜 . 如果有人能帮助我解决这些问题,我们将非常高兴地感激不尽 .

2 回答

  • 1

    很难从你的图像中看出,但我几乎是100%你没有正确关闭表标签,你所描述的正是在这种情况下会发生什么 .

    问题是你在一个迭代点(foreach)上打开一个表标记,然后在下一个迭代点添加2行,ok,到目前为止,在open table标记内添加一个h2标记,依此类推 .

    浏览器将尝试通过关闭标记为您修复这些标记,但它不会为您重新排列html,因此h标记在渲染视图中的表格上方可见 .

    见下文:只有有帖子才会打开表格 . 如果你想在没有帖子时打开一个,你需要在宣布“找不到帖子”等之前关闭 table

    <?php
    
    $args2 = array( 'taxonomy'  =>   'product_cat', 'parent' => 9 );
    $sub_cats = get_categories( $args2 );
    
    foreach( $sub_cats as $sub_category ) { ?>
    
        <div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>">
    
            <h2 class="section-heading">
                <span class="line-behind-text"><?php echo $sub_category->name;?></span>
            </h2>
    
            <p class="section-text">
                OPIS: <?php echo category_description(); ?> //this part does not work too, not sure why
            </p>
    
            <h3 class="section-heading"><p class="line-behind-text">Dostepne zabiegi</p></h3>
            <?php
    
                $name = $sub_category->name;
                $args = array( 'post_type' => 'product', 
                               "product_cat" => $sub_category->term_id //PROBLEM HERE
                        );
    
                 $loop = new WP_Query( $args );
    
                 if ( $loop->have_posts() ) {
                    //only include the table if we have posts???
    
                        echo '<table class="treatments-table table products">';
    
                        echo '<tr class="table-heading">
                            <th class="name" id="<?php echo $sub_category->term_id;?>">Usluga</th>
                            <th>Czas trwania</th>
                            <th>Cena</th>
                            <th></th>
                            </tr>';
    
    
    
                     while ( $loop->have_posts() ) : $loop->the_post();
    
                         $product = new WC_Product(get_the_ID()); ?>
    
                         <tr>
                             <td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td>
                             <td><?php the_excerpt(); ?></td>
                             <td><?php echo $product->price; ?>zl</td>
                             <td><button class="button-product materialbutton">Rezerwuj</button> </td>
                        </tr> <?php
    
                     endwhile;
    
                    echo '</table>'; 
    
    
                 } 
                 else {
    
                     echo __( 'No products found' );
                 } ?>
    
                 <h1>THE END</h1> <?php
    } //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS -- must close tags in the loop, otherwise multiple open tags!!
    
  • 1
    • 对于您的第一个问题,我很确定您需要将 category_description(); 替换为 $sub_category->description; (在下面的代码中是 $sub_cat->description;

    • 对于你的第二个问题,我无法测试它,我不完全确定,但你需要更多的东西( see in here )......

    • 您还需要使用 </table> (接近结尾)和 </div> (结尾)关闭表格

    <?php
    
        $args2 = array( 'taxonomy'  =>   'product_cat', 'parent' => 9 );
        $sub_cats = get_categories( $args2 );
    
        foreach( $sub_cats as $sub_cat ) { ?>
            $sub_cat_name = $sub_cat->name;
            $sub_cat_description = $sub_cat->description; // <= Here (1)
            $sub_cat_id = $sub_cat->term_id;
    
        <div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>">
    
            <h2 class="section-heading">
                <span class="line-behind-text"><?php echo $sub_cat_name; ?></span>
            </h2>
    
            <p class="section-text">
                OPIS: <?php echo $sub_cat_description; ?>
            </p>
    
            <h3 class="section-heading"><p class="line-behind-text">Dostępne zabiegi</p></h3>
    
            <table class="treatments-table table products">
    
                <tr class="table-heading">
                    <th class="name" id="<?php echo $sub_cat_id; ?>">Usługa</th>
                    <th>Czas trwania</th>
                    <th>Cena</th>
                    <th></th>
                </tr>
            <?php
    
                global $post; // Here (2)
                $terms = get_the_terms( $post->ID, 'product_cat' );
                foreach ($terms as $term) {
                    $product_cat_id = $term->term_id;
                    // $product_cat_name = $term->name;
                    break;
                }
                $args = array( 
                    'post_type' => 'product', 
                    'product_cat' => $product_cat_id'
                );
    
                $loop = new WP_Query( $args );
    
                if ( $loop->have_posts() ) {
    
                    while ( $loop->have_posts() ) : $loop->the_post();
    
                        $product = new WC_Product(get_the_ID()); ?>
    
                <tr>
                    <td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td>
                    <td><?php the_excerpt(); ?></td>
                    <td><?php echo $product->price; ?>zł</td>
                    <td><button class="button-product materialbutton">Rezerwuj</button> </td>
                </tr>
                <?php 
                    endwhile; ?>
            </table>
            <?php 
                } 
                else {
                    echo __( 'No products found' );
                } ?>
    
             <h1>THE END</h1> 
             <?php
             } 
             ?>
         </div>
    

相关问题