【发布时间】:2011-12-03 20:33:54
【问题描述】:
在下面的代码中有一个循环显示给定类别下的所有产品:
<?php
wp_reset_query();
query_posts($query_string . '&posts_per_page=15&paged=' . $paged);
if (have_posts()) :
while ( have_posts() ) : the_post();
$price = get_post_meta(get_the_ID(), 'inception_price', true);
?>
<div class="omc-product-listing">
<a href="<?php the_permalink();?>">
<?php
if(has_post_thumbnail()) {
the_post_thumbnail('product', array('class' => 'omc-product-frame'));
} else {
?>
<img src="<?php echo get_template_directory_uri() ;?>/images/no-image.png" width="170" height="170" class="omc-product-frame" alt="no photo" />
<?php } ?>
</a>
<span class="omc-listing-header"><a href="<?php the_permalink();?>"><?php the_title();?></a></span>
<span class="omc-listing-price"><?php echo($price);?></span>
<span class="omc-listing-more"><a href="<?php the_permalink();?>">»</a></span>
</div><!-- /omc-product-listing -->
<?php endwhile; ?>
<br class="clear" />
<div class="product-pagination">
<?php kriesi_pagination(); ?>
</div>
<br class="clear" />
<?php endif; wp_reset_query(); ?>
在这个循环中,我想提取给定类别的每个产品(这就是代码现在所做的),然后显示“每个子类别”,如下所示:
对于类别出版物:
书籍:
- 第 33 册
- 第 32 册
- 书 1
- ...
移动应用:
- 应用 12
- 应用 76
- ...
...
我认为上面的代码需要一个foreach 循环,如下所示,但我不知道在这种情况下如何实现它。
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categories
foreach ($cats as $cat) {
// setup the categories ID
$cat_id= $cat->term_id;
// Make a header for the categories
echo "<h2>".$cat->name."</h2>";
// create a custom wordpress query
query_posts("cat=$cat_id&post_per_page=100");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php echo '<hr/>'; ?>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
【问题讨论】:
标签: php wordpress loops foreach categories