1
對於單個頁面,我需要更改代碼以僅獲取特定類別的項目列表。所以喜歡:選擇食品類別爲15的項目。在Wordpress中獲取數組中的特定帖子
我嘗試過不同的事情,但我不擅長於PHP。所以你可以幫我改變一下代碼。這是列表發佈的部分。
<div class="col-md-6">
<div class="box_style_2" id="main_menu">
<h2 class="inner">Menu</h2>
<?php
$terms = get_terms('foodcat', array(
'include' => $category_food,
));
?>
<?php
$i = 1;
foreach($terms as $term){ ?>
<h3 <?php if($i=1){?>class="nomargin_top"<?php }else{}?> id="<?php echo esc_attr($term->slug);?>"><?php echo esc_attr($term->slug);?></h3>
<p><?php echo esc_attr($term->description);?></p>
<table class="table table-striped cart-list">
<thead>
<tr>
<th>
<?php echo esc_html__('Item', 'quickfood');?>
</th>
<th>
<?php echo esc_html__('Price', 'quickfood');?>
</th>
<th>
<?php echo esc_html__('Order', 'quickfood');?>
</th>
</tr>
</thead>
<tbody>
<?php
$food_arr = new WP_Query(
array(
'post_type' => 'food',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'foodcat',
'field' => 'id',
'terms' => $term->term_id,
)
)
)
);
$ii = 1;
if($food_arr->have_posts()) : while($food_arr->have_posts()) : $food_arr->the_post();
$textmoney = get_post_meta(get_the_id(), '_cmb2_textmoney', true);
?>
<tr id="detail-<?php the_id();?>">
<input type="hidden" value="<?php the_id();?>">
<td>
<h5><?php echo esc_attr($ii);?>.<span class="name-<?php the_id();?> item_name"><?php the_title();?></span>
<input type="hidden" id="name-<?php the_id();?>" value="<?php the_title();?>">
</h5>
<p>
<?php the_content();?>
</p>
</td>
<td>
<strong><?php if(isset($currency_menu) && !empty($currency_menu)){ echo esc_attr($currency_menu);}else{}?><span class="price-<?php the_id();?> price"> <?php echo esc_attr($textmoney);?></span></strong>
<input type="hidden" id="price-<?php the_id();?>" value="<?php echo esc_attr($textmoney);?>">
</td>
<td class="options">
<a href="#0"><i class="icon_plus_alt2 add-to-cart-button" id="<?php the_id();?>"></i></a>
</td>
</tr>
<?php $ii++; endwhile;endif;?>
</tbody>
</table>
<hr>
<?php } ?>
</div>
</div>
你應該使用get_posts()並將其限制爲一個類別,否? https://codex.wordpress.org/Template_Tags/get_posts – elicohenator