In the below code there’s a loop that features all products under a given category:
<?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(); ?>
In this loop, I’d like to pull out every product for a given category (that’s what the code does now), but display then “per subcategory”, like this:
For category publications:
Books:
- Book 33
- Book 32
- Book 1
- …
Mobile Apps:
- App 12
- App 76
- …
…
I think the code above needs a foreach
loop, like below, but I don’t know how to implement it in this case.
<?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 ?>
I’ve taken this request to wpquestions.com. It just got resolved by Abdessamad Idrissi. the answer is too long to copy it here, so I’m posting the discussion link and the code link here, in case someone has the same need.
You could try the following:
Not tested this but it should put you in the right direction!