php - Wordpress - wp_query inside of a foreach loop -
i using acf build product catalogue. need create list that:
+category name
-list
-of
-products
-in
-that
-category
+another category name
-list
-of
-...
-in matching category
of categories.
categories names terms of 'product_categories' taxonomy. products belong custom post type 'products'.
i've build loop displays links of categories names , images:
$terms = get_terms("product_categories", array('hide_empty' => false)); if ( !empty( $terms ) && !is_wp_error( $terms ) ){ foreach ( $terms $term ) { echo '<a href="'.get_term_link($term).'">'; echo '<p>'.$term->name.'</p>'; echo '<img src="'.get_field('image-field-name', $term).'"></a></div>'; } }
it works fine.
rebuilded display thing want:
$terms = get_terms("product_categories", array('hide_empty' => false)); if ( !empty( $terms ) && !is_wp_error( $terms ) ){ foreach ( $terms $term ) { $category= $term->name; echo '<p>'.$category.':</p>'; product_list($category); echo '<br/><br/>'; } } function product_list($category_name){ $inner_args=array( 'post_type' => 'products', 'product_categories' => $category_name ); $my_query = null; $my_query = new wp_query($inner_args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <?php echo '<br/>product: '.get_field('name_of_the_product'); ?> <?php endwhile; } }
it works. the problem displays of product names first category, after of products first category in loop echoed, following categories empty (just category names, no matching items).
how can make display products in of categories, not first one? , what's wrong code?
have tried adding wp_reset_query();
before end of foreach loop?
more info http://codex.wordpress.org/function_reference/wp_reset_query
Comments
Post a Comment