Last active
October 17, 2015 11:38
-
-
Save jitenbharadava/91fe2e9ef3e6b2f7c4d9 to your computer and use it in GitHub Desktop.
Terms location serach
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 | |
function print_show_location( $terms = '' ){ | |
// check input | |
if ( empty( $terms ) || is_wp_error( $terms ) || ! is_array( $terms ) ) | |
return; | |
// set id variables to 0 for easy check | |
$country_id = $state_id = $city_id = 0; | |
// get country | |
foreach ( $terms as $term ) { | |
if ( $country_id || $term->parent ) | |
continue; | |
$country_id = $term->term_id; | |
$country_slug = $term->slug; | |
$country = '<a href="'.get_term_link($country_slug, 'location').'">'.$term->name.'</a>'; | |
} | |
// get state | |
foreach ( $terms as $term ) { | |
if ( $state_id || $country_id != $term->parent ) | |
continue; | |
$state_id = $term->term_id; | |
$state_slug = $term->slug; | |
$state = '<a href="'.get_term_link($state_slug, 'location').'">'.$term->name.'</a>'; | |
} | |
// get city | |
foreach ( $terms as $term ) { | |
if ( $city_id || $state_id != $term->parent ) | |
continue; | |
$city_id = $term->term_id; | |
$city_slug = $term->slug; | |
$city = '<a href="'.get_term_link($city_slug, 'location').'">'.$term->name.'</a>'; | |
} | |
// output | |
echo "$city, $state - $country"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment