Created
May 1, 2020 17:00
-
-
Save rustam-swe/d20837c6e077b11516c3141a028283b7 to your computer and use it in GitHub Desktop.
Wordpress snippet to get all categories except the first level
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
$args = [ | |
'taxonomy' => 'product', // Taxonomy type | |
'hide_empty' => false, // Display all categories including empty ones too | |
'exclude' => 15 // ID of default, uncategorized category or any taxonomy (optional) | |
]; | |
$topLevelCategories = get_categories($args); // Query to get all categories | |
$childCategories = getChildCategories($topLevelCategories); | |
function getChildCategories($categories) { | |
$childCategories = []; | |
foreach ($categories as $category) { | |
if ($category->parent != 0) { | |
$childCategories[] = $category; | |
} | |
} | |
return $childCategories; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment