我找到了这个问题的答案。使用原始代码的简化版本,我可以传入一个类别以仅显示该类别,如果为空,则显示所有类别。然后,foreach条件遍历所有可用类别,并在查询期间耗尽每个类别中的帖子。
我希望这在某些方面对某人有所帮助:)
//MENU SHORTCODE
add_shortcode(\'himenu\', \'create_menu_shortcode\');
function create_menu_shortcode ($atts){
$atts = shortcode_atts(
array(
\'category\' => \'\'
), $atts, \'himenu\' );
$category = $atts["category"];
?> <?php
var_dump ($atts);
var_dump ($category);
$args_cat = [
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 0,
];
$menu_categories = get_categories($args_cat);
//print_r($categories);
if (!empty($menu_categories)):
foreach ($menu_categories as $menu_category):
$args = [
\'post_type\' => \'menu_item\',
\'posts_per_page\' => -1,
\'order\' => \'ASC\',
\'orderby\' => \'title\',
\'category_name\' => $category,
\'cat\' => $menu_category->term_id
];
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post(); ?>
<div class="indi-menu-item">
<div class="menu-item-column">
<h5> <?php the_category(); ?></h5>
<div class="menu-item-meta">
<h4> <?php the_title(); ?></h4>
<p><?php the_field(\'description\')?></p>
</div>
</div>
<div class="menu-item-column">
<h2>£<?php the_field(\'price\'); ?></h2>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
endforeach;
endif;?>
</div>
<?php } ?>
<?php wp_reset_query();//reset the global variable related to post loop
?>