Skip to content

Instantly share code, notes, and snippets.

@robneu
Created January 2, 2013 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robneu/4439393 to your computer and use it in GitHub Desktop.
Save robneu/4439393 to your computer and use it in GitHub Desktop.
A quick and dirty method for displaying a hierarchical list of taxonomy terms for a custom taxonomy called 'location'. This could/should be built out more to be more dynamic and future-friendly.
<?php
// Display taxonomy terms ordered from child to parent
function csi_ordered_tax( $terms ) {
// if terms is not array or its empty don't proceed
if ( ! is_array( $terms ) || empty( $terms ) ) {
return false;
}
foreach ( $terms as $term ) {
// if the term have a parent, set the child term as attribute in parent term
if ( $term->parent != 0 ) {
$terms[$term->parent]->child = $term;
} else {
// record the parent term
$parent = $term;
}
}
$rooturl = get_bloginfo( 'url' );
$child_link = '<a href="' . $rooturl . '/open-jobs?location=' . $parent->child->name . '">' . $parent->child->name . '</a>';
$parent_link = '<a href="' . $rooturl . '/open-jobs?location=' . $parent->name . '">' . $parent->name . '</a>';
return "{$child_link}, $parent_link";
}
// [csilocations taxonomy="location"]
function csi_get_ordered_tax( $atts ) {
extract( shortcode_atts( array(
'taxonomy' => 'location',
), $atts ) );
global $post;
$tax_list = csi_ordered_tax( get_the_terms( $post->ID, $taxonomy ) );
return $tax_list;
}
add_shortcode( 'csilocations', 'csi_get_ordered_tax' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment