【发布时间】: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