我正在尝试创建一个显示外卖业务网站自定义查询(调用菜单项)结果的快捷码。我已经尝试让类别属性发挥作用,以便在主页上可以使用[himenu cat=“special offers”],这是我用来显示特价商品的类别的鼻涕虫。这很好,没有问题,但是我正在努力实现以下内容(如下面的代码所示)
1-在“完整菜单”页面上,我想使用显示所有菜单项的相同快捷码,只需将cat属性留空2-在完整菜单页面上,列出相应类别标题下的所有菜单项,例如:。
BURGERSitem 1项目2项目3项目4
烤肉串1项目2项目3
等
我已经为自定义帖子类型设置了类别,所以我想我需要找到类别并在查询运行之前循环它们?任何建议都很好,我花了整整一个上午在thsi上都没有成功。
谢谢
//MENU SHORTCODE
add_shortcode( \'himenu\', \'cat_post\' );
function cat_post($atts){
// attributes for shortcode
if (isset($atts[\'cat\'])) {$cat = $atts[\'cat\'];} else {return;}
if (isset($atts[\'posts_per_page\'])) {$posts_per_page = $atts[\'posts_per_page\'];} else {$posts_per_page = -1;}
// get the category posts
$category = get_category_by_slug($cat);
if (!is_object($category)) {return;}
$args = array(
\'cat\' => $category->term_id,
\'post_type\' => menu_item,
\'posts_per_page\' => $posts_per_page,
\'order\' => \'ASC\',
\'orderby\' => \'title\'
);
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post(); ?>
<div class="indi-menu-item">
<div class="menu-item-column">
<h5> <?php
if($cat === \'special-offers\'){
echo \'Special Offers\';
} else {
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(); // reset the query
}
wp_reset_query();//reset the global variable related to post loop
?>