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

WooCommerce: products-of-category loop bizarre problems

我编写了一个 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

现在理论上它应该像这样显示 divs:

  1. 类别a: 1a.品类说明 1b.类别 table
  2. b 类: 2a.品类说明 2b.类别 table

但是,结果是这样的:

因此,正如您所见,它不仅没有正确布局页面(顺序是 descrption1、description2、table1、table2,注意 <h1>THE END</h1> 的位置),它似乎也没有正确匹配类别的产品。当 Id 放入数组

时会发生相同的结果

"product_cat" => 14 //proven category id, containing posts

我在 wp 方面很有经验,但对 woocommerce 还是个新手。如果有人可以帮助我解决这些问题,将不胜感激。

很难从你的图片中看出,但我几乎 100% 认为你没有正确关闭 table 标签,你所描述的正是这种情况下会发生的情况。

问题是你在一个迭代点 (foreach) 上打开一个 table 标签,然后添加 2 行,到目前为止,在下一个迭代点上,你在打开的 [=21] 中添加一个 h2 标签=] 标签等等。

浏览器会尝试通过关闭标签来为您修复这些问题,但它不会为您重新排列 html,因此 h 标签在呈现的 table 上方可见看法。

见下文: table 只有在有帖子时才会打开。如果你想在没有帖子时打开一个,你需要在声明 "no posts found" 等

之前关闭 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;

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

  3. 您还需要用 </table>(接近尾声)和 </div>(在尾声)关闭您的 table


<?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>