Created
April 13, 2023 10:42
-
-
Save woogists/0e4e531313c9096637651e355e8f895d to your computer and use it in GitHub Desktop.
Top-level Product Category List Shortcode
This file contains 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 | |
/* | |
* The shortcode is [top_level_product_categories_list] | |
*/ | |
add_shortcode('top_level_product_categories_list', 'wc_shortcode_top_level_product_categories_list'); | |
function wc_shortcode_top_level_product_categories_list() { | |
ob_start(); | |
$args = array( | |
'taxonomy' => 'product_cat', | |
'parent' => 0 | |
); | |
$product_categories = get_categories($args); | |
echo '<ul>'; | |
foreach ($product_categories as $category) { | |
echo '<li><a href="' . get_term_link($category->slug, 'product_cat') . '">' . $category->name . '</a></li>'; | |
} | |
echo '</ul>'; | |
return ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment