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

时间:2016-05-08 12:54:25

标签: php wordpress woocommerce categories product

我有一个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:

  1. 类别a: 1A。类别说明 1B。类别表
  2. 类别b: 2A。类别说明 2B。类别表
  3. 但相反,结果如下: Outcome

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

    时会发生相同的结果
      

    &#34; product_cat&#34; =&GT; 14 //经过验证的类别ID,包含帖子

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

2 个答案:

答案 0 :(得分:1)

很难从你的图片中看出,但我差不多100%你没有正确关闭表格标签,你所描述的正是在这种情况下会发生的事情。

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

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

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

<?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 :(得分:1)

  1. 对于您的第一个问题,我非常确定您需要category_description();替换$sub_category->description;(在下面的代码中为$sub_cat->description;

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

  3. 您还需要使用</table>(接近结尾)和</div>(最后)关闭您的表格


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