Created
April 23, 2014 13:35
-
-
Save ivandoric/11215420 to your computer and use it in GitHub Desktop.
wordpress: Get last child category ID
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 | |
//Get all terms associated with post in taxonomy 'category' | |
$terms = get_the_terms(get_the_ID(),'category'); | |
//Get an array of their IDs | |
$term_ids = wp_list_pluck($terms,'term_id'); | |
//Get array of parents - 0 is not a parent | |
$parents = array_filter(wp_list_pluck($terms,'parent')); | |
//Get array of IDs of terms which are not parents. | |
$term_ids_not_parents = array_diff($term_ids, $parents); | |
//Get corresponding term objects | |
$terms_not_parents = array_intersect_key($terms, $term_ids_not_parents); | |
//print_r($terms_not_parents); | |
echo key($terms_not_parents); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment