I
I
IG512016-11-09 10:56:00
Online shopping
IG51, 2016-11-09 10:56:00

How to display products in categories on woocommerce map site?

Guys tell me how to make a sitemap in the form of a tree. Ordinary pages, headings, posts - it's all clear. But it is not possible to display woocommerce products in the same form.

<ul>
<?php

$taxonomyName = "product_cat";
$prod_categories = get_terms($taxonomyName, array(
    'orderby'=> 'name',
    'order' => 'ASC',
    'hide_empty' => 1
));  
// Add categories you'd like to exclude in the exclude here
$cats = get_categories('exclude=');
foreach ( $prod_categories as $prod_cat ) {
  echo "<li><h3>Категория: <a href=\"".get_category_link($prod_cat->term_id)."\" target=\"_blank\" >".$prod_cat->name."</a></h3>";
  echo "<ul>";
  // вот здесь бы вывести продукты под каждую категорию...
  //  query_posts('posts_per_page=-1&prod_cat='.$prod_cat->prod_cat_ID);
  // while(have_posts()) {
  //   the_post();
  //   $category = get_the_category();
  //   // Only display a post link once, even if it's in multiple categories
  //   if ($category[0]->prod_cat_ID == $prod_cat->prod_cat_ID) {
  //     echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
  //   }
  // }

  echo "</ul>";
  echo "</li>";
}
?>
</ul>

This code outputs product categories, but what is commented out displays normal entries, not products related to the product category.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-11-09
@HeadOnFire

1. Never use query_posts()
2. Use get_posts() instead of query_posts() in this case
3. You forgot the extra parameter post_type=product, post_type=post is displayed without it (default value)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question