Forked from OneStep21/GET ALL CATEGORIES IN WOOCOMMERCE
Created
May 22, 2018 11:18
-
-
Save omusegad/0570bb852ac861a9ab92f2bcc3bbc478 to your computer and use it in GitHub Desktop.
HOW TO GET ALL CATEGORIES , SUB-CATEGORIES AND PRODUCTS IN WOOCOMMERCE WORDPRESS PLUGIN ( 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. )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
$args = array( | |
'taxonomy' => $taxonomy, | |
'orderby' => $orderby, | |
'show_count' => $show_count, | |
'pad_counts' => $pad_counts, | |
'hierarchical' => $hierarchical, | |
'title_li' => $title, | |
'hide_empty' => $empty | |
); | |
$all_categories = get_categories( $args ); | |
foreach ($all_categories as $cat) { | |
if($cat->category_parent == 0) { | |
$category_id = $cat->term_id; | |
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; ?> | |
$args2 = array( | |
'taxonomy' => $taxonomy, | |
'child_of' => 0, | |
'parent' => $category_id, | |
'orderby' => $orderby, | |
'show_count' => $show_count, | |
'pad_counts' => $pad_counts, | |
'hierarchical' => $hierarchical, | |
'title_li' => $title, | |
'hide_empty' => $empty | |
); | |
$sub_cats = get_categories( $args2 ); | |
if($sub_cats) { | |
foreach($sub_cats as $sub_category) { | |
echo $sub_category->name ; | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment