Show 1 product from each category
-
I am using Woocommerce and trying to display 1 product from each product category (as an example).
<?php
$taxonomy = ‘product_cat’;
$orderby = ‘name’;
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = ”;
$empty = 0;
$myargs = array(
‘taxonomy’ => $taxonomy,
‘orderby’ => $orderby,
‘show_count’ => $show_count,
‘pad_counts’ => $pad_counts,
‘hierarchical’ => $hierarchical,
‘title_li’ => $title,
‘hide_empty’ => $empty
);
?>
<?php $all_categories = get_categories( $myargs );
foreach($all_categories as $category) {
$posts=get_posts(‘showposts=1&cat=’. $category->term_id);
if ($posts) {
echo ‘<p>Category: term_id ) . ‘” title=”‘ . sprintf( __( “View all posts in %s” ), $category->name ) . ‘” ‘ . ‘>’ . $category->name.’ </p> ‘;
foreach($posts as $post) {
setup_postdata($post); ?>
<p>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>This is my code. If I take the taxonomy out, then it work perfect with the blog posts. But when I use the Woocommerce taxonomy of product_cat, it displays nothing.
- The topic ‘Show 1 product from each category’ is closed to new replies.