jengalas
11/8/2013 - 10:22 PM

Orders three-level hierarchical WordPress taxonomy (State -> County -> Map Quad 'location' taxonomy, in my case). Put this in your theme's f

Orders three-level hierarchical WordPress taxonomy (State -> County -> Map Quad 'location' taxonomy, in my case). Put this in your theme's functions.php file, and use the shortcode wherever you need it.

Huge thanks to Hameedullah Khan for his contribution on WordPress Answers (http://wordpress.stackexchange.com/questions/37285/custom-taxonomy-get-the-terms-listing-in-order-of-parent-child). I was able to use his code almost verbatim, and simply adapt the particulars to my project.

<?php

function order_location_terms() {    
   	 
    $taxonomy_slug = "location";			 
    $terms = get_the_terms( $post -> ID, $taxonomy_slug);
	 
    if ( ! is_array( $terms ) || empty( $terms ) ) {
        return false;
    }

    foreach ( $terms as $term ) {
        if ( $term -> parent != 0 )  {
            $terms[$term -> parent] -> child = $term;   
        } else {
            $parent = $term;
        }
    }

		$state_link = '/' . $taxonomy_slug . '/' . $parent -> slug;
		$county_link = $state_link . '/' . $parent -> child -> slug;
		$quad_link = $county_link . '/' . $parent -> child -> child -> slug; 
		
		return "<a href=$state_link>$parent -> name</a> - <a href=$county_link>" . $parent -> child -> name . "</a> - <a href=$quad_link>" . $parent -> child -> child -> name . "</a>";
		
}

add_shortcode( 'location_tax_inorder', 'order_location_terms' );

?>