I am trying to get the product categories from WooCommerce through a function in my WordPress theme
function get_me_list_of($atts, $content = null)
{
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0]);
$loop = new WP_Query( $args );
echo '<h1 class="upp">Style '.$atts[0].'</h1>';
echo "<ul class='mylisting'>";
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<li><a href="'.get_permalink().'">'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</a></li>';
echo '<li><a href="'.get_permalink().'">'.$loop->post->post_title.'</a></li>';
echo '<li><a href="">'.get_categories().'</a></li>';
endwhile;
echo "</ul>";
wp_reset_query();
}
?>
The above code returns some products, but the product categories.
When I included echo '<li><a href="">'.get_categories().'</a></li>';
in the code above it returns as an array. How do I fix this?
How do i change this to get the product categories from WooCommerce?
Improving Suman.hassan95's answer by adding a link to subcategory as well. Replace the following code:
with:
or if you also wish a counter for each subcategory, replace with this:
This will list all the top level categories and subcategories under them hierarchically. do not use the inner query if you just want to display the top level categories. Style it as you like.