<div class="pr_grid_cell clearfix">
<?php
//for each category, show posts
$categories=get_categories();
foreach($categories as $cat) {
$args=array(
\'post_type\' => \'products\',
\'showposts\' => -1,
\'category__in\' => array($cat->term_id)
);
$posts = new wp_query($args); // This is the best way to
if ($posts->have_posts()) while ($posts->have_posts()) { //condional if/while 2-in-1. If you dont check have_posts, you\'ll get an ugly error when nothings found.
the_post();//increments wps internal counter, Needed so that your loop doesnt run forever.
?>
<div class="pr_grid buy-online">
<span class="hcenter">
<h5><?= $cat->name ?></h5>
<? foreach($posts as $post) {
setup_postdata($post); ?>
<? if( get_field(\'buy_online_href\') ): ?>
<?php // create our link now that the post is setup ?>
<span class="pr_img_href" href="<? the_permalink(); ?>">
<img class="ii" src="<?php the_field(\'product_thumbnail\'); ?>">
<a class="button" target="_blank" href="<?php the_field(\'buy_online_href\'); ?>">Buy Online</a>
<a class="button" target="_blank" href="/store-locator">Find Near you</a>
</span>
<? endif ?>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
</span>
</div>
</div>
如上所述,避免使用query\\u帖子。在几乎所有情况下,只实例化自己的wp\\u查询实例更为简洁。我认为缺少一件事,这就是为什么它会无限运行,那就是缺少了\\u post()。这会增加“计数器”,告诉循环何时完成。没有它,它将无限期地运行。