【问题标题】:PHP - Loop custom post type categories within jQuery TabsPHP - 在 jQuery 选项卡中循环自定义帖子类型类别
【发布时间】:2018-01-21 17:40:48
【问题描述】:

我有一个名为“产品”的自定义帖子类型

在产品中有一个名为“子类别”(selsubcat) 的自定义字段,其中包含 14 个不同的类别。

这些类别是; CAM / REC / ACC / SWH / NET / MON / HDMI / ENC / SIGNS / PIR / WIFI / POLE / SOUND。

我正在尝试在不同的选项卡中显示所有产品,其中选项卡是子类别,每个选项卡都将显示该类别的产品。目前我已将meta_value 定义为“CAM”,这将在#tabs-1 div 中显示所有具有“CAM”类别的产品 - 这是正确的。

我现在需要以某种方式循环每个类别并在其他 13 个选项卡中显示产品。

所以#tabs-1 将显示“CAM”类别的所有产品,#tabs-2 将显示“REC”类别的所有产品等等...

这是我目前拥有的:-

<div id="tabs">
    <ul>
        <?php
        if( have_rows('sub_category', 'option') ): $cat_increment = 1;
            while ( have_rows('sub_category', 'option') ) : the_row(); ?>
                <!-- Tab Sub Categories -->
                <li><a href="#tabs-<?php echo $cat_increment; ?>"><?php the_sub_field('category_name'); ?></a></li>
            <?php $cat_increment++; endwhile;
        else : endif;
        ?>
    </ul>

    <?php 

    $posts = get_posts(array(
        'posts_per_page'=> -1,
        'post_type'     => 'products',
        'order'             => 'ASC',
        'meta_key'      => 'selsubcat',
        'meta_value'    => 'CAM'
    ));

    if( $posts ): ?>


    <div id="tabs-1">   

        <table id="table-products" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th class="hidden">id</th>
                    <th>Image</th>
                    <th>Product Code</th>
                    <th>Description</th>
                    <th>Cost</th>
                    <th>Qty</th>
                    <th>Total Cost</th>
                    <th>Options</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th class="hidden">id</th>
                    <th>Image</th>
                    <th>Product Code</th>
                    <th>Description</th>
                    <th>Cost</th>
                    <th>Qty</th>
                    <th>Total Cost</th>
                    <th>Options</th>
                </tr>
            </tfoot>                        

                <?php foreach( $posts as $post ): 

                    setup_postdata( $post );

                    ?>

                    <tbody>
                          <tr>
                              <td class="hidden"><?php echo $row['id']; ?></td>
                              <td><span class="product-image" style="background:url(<?php the_field('image'); ?>);"></span></td>
                              <td><?php the_field('code'); ?></td>
                              <td><?php the_field('description'); ?></td>
                              <td><?php the_field('cost_price'); ?></td>
                              <td><input type="text" /></td>
                              <td><input type="text" /></td>
                              <td><input type="button" class="add-row" name="add-row" value="Add"></td>
                          </tr>
                    </tbody>

                <?php $product_increment++; endforeach; ?>

            </table>    

        </div>


        <?php wp_reset_postdata(); ?>

    <?php endif; ?>

</div>

我被困在这一点上,所以任何帮助将不胜感激!

【问题讨论】:

    标签: php wordpress loops foreach while-loop


    【解决方案1】:

    首先将所有类别 id 存储在一个数组中,然后在 foreach 循环中获取每个类别名称和帖子并将它们保存在另一个数组中(类别名称作为键,帖子作为值),现在您有了一个存储所有类别的数组里面的帖子和类别!

    $categories_id = array(1,2,3,4,5,6,7,8,9,10,11,12,13);
    $posts = array();
    foreach ($categories_id as $id) {
        $term = get_term($id,'product_category');
        $term_name = $term->name;
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_category',
                    'field'    => 'id',
                    'terms'    => $id
                )
            )
        );
        $query = new WP_Query( $args );
        while($query->have_posts()){
            $query->the_post(); 
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumbnail' );
            $posts[$term_name][] = array(
                                        'title' => get_the_title(),
                                        'link'  => get_the_permalink(),
                                        'image' => $image[0]
                                    );
        }
        wp_reset_query();
    }
    

    将数组打印到标签中:

    <div class="tabs">
        <ul class="tab-links">
            <?php
                $count = 0;
                $class = "active";
                foreach ($posts as $key => $value) : ?>
                    <li class="<?php echo $class; ?>"><a href="#tab<?php echo $count; ?>"><?php echo $key; ?></a></li>
            <?php
                    if($count == 0)
                        $class = "";
                    $count++;
                endforeach; ?>
        </ul>
        <div class="tab-content">
            <?php
                $count = 0;
                $class = "active";
                foreach ($posts as $key => $value): ?>
                    <div id="tab<?php echo $count; ?>" class="tab <?php echo $class; ?>">
                    <?php
                        foreach ($posts[$key] as $post) : 
                            //echo post['title']
                            //echo post['link']
                            //echo post['image']
                        endforeach; ?>
                    </div>
            <?php
                    if($count == 0)
                        $class = "";
                    $count++;
                endforeach; ?>
        </div>
    </div>
    

    【讨论】:

    • 感谢您的回复@aidinMC - 我无法让它继续工作,基本上我只需要用不同的元值循环每个类别,所以'REC'将在
      ,'ACC' 将在
      内,SWH 将在
      内div> 等等。
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2018-06-19
    • 2017-07-29
    • 1970-01-01
    相关资源
    最近更新 更多