【问题标题】:WooCommerce: products-of-category loop bizarre problemsWooCommerce:产品类别循环奇怪的问题
【发布时间】:2016-09-03 03:51:39
【问题描述】:

我编写了一个 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。类别表

但是,结果却是这样的:

因此,正如您所看到的,它不仅没有正确布置页面(顺序为 descrption1、description2、table1、table2,注意&lt;h1&gt;THE END&lt;/h1&gt; 的位置),它似乎也没有正确匹配产品类别。当 Id 放入数组时会发生相同的结果

"product_cat" => 14 //经过验证的类别id,包含帖子

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

【问题讨论】:

  • 实际上只是一点,您有很多未回答/接受的答案的未决问题?

标签: php wordpress woocommerce categories product


【解决方案1】:

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

问题是您在一个迭代点(foreach)上打开一个表标签,然后添加 2 行,到目前为止,在下一个迭代点上,您在打开的表标签内添加一个 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!!

【讨论】:

  • 您的代码有效,除了显示产品之外的所有内容。即使正确显示类别信息,它也会给我“未找到产品”
  • 即使我给 args 一个静态的“product_cat”=> 13,问题似乎也是一样的,其中 13 是包含产品的猫,至少根据 wp-admin 的说法
  • 已修复!问题是 product_cat 使用名称而不是 id,因此使用 ` $args = array( 'post_type' => 'product', "product_cat" => $sub_category->name //PROBLEM HERE );` 工作得很好
【解决方案2】:
  1. 对于您的第一个问题,我很确定您需要将category_description(); 替换为$sub_category-&gt;description;(在下面的代码中为$sub_cat-&gt;description;

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

  3. 您还需要使用&lt;/table&gt;(接近尾声)和&lt;/div&gt;(在尾声)关闭表


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 2019-02-06
    • 1970-01-01
    • 2019-04-25
    • 2018-06-09
    • 1970-01-01
    • 2018-12-19
    • 2021-06-13
    相关资源
    最近更新 更多