Last active
February 17, 2016 18:01
-
-
Save chrdesigner/b2a83f407b0dc6469229 to your computer and use it in GitHub Desktop.
List All Categories and Subcategories and return the last post register
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 | |
/** | |
* Returns ID of top-level parent category, or current category if you are viewing a top-level | |
* | |
* @param string $catid Category ID to be checked | |
* @return string $catParent ID of top-level parent category | |
*/ | |
function smart_category_top_parent_id ($catid) { | |
while ($catid) { | |
$cat = get_category($catid); // get the object for the catid | |
$catid = $cat->category_parent; // assign parent ID (if exists) to $catid | |
// the while loop will continue whilst there is a $catid | |
// when there is no longer a parent $catid will be NULL so we can assign our $catParent | |
$catParent = $cat->cat_ID; | |
} | |
return $catParent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment