Last active
August 29, 2015 14:04
-
-
Save johnnyicarus/8f956fe43e129eb7337b to your computer and use it in GitHub Desktop.
WordPress utilities, allows the separate display of explicit levels of hierarchial taxonomies
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
/** | |
* Template Tag: the_terms_by_level | |
* | |
* Echoes the links of all the terms on the same explicit hierarchical level | |
* associated with a certain taxonomy. You should pass a separator. | |
* | |
* @param $id int post id | |
* @param $taxonomy string the taxonomy | |
* @param $level int level of the hierarchy to echo (top level = 0) | |
* @param $sep string separator | |
* @return void | |
* @uses get_the_terms() | |
* @uses get_ancestors() | |
* @uses get_term_link() | |
*/ | |
function prefix_the_terms_by_level( $id, $taxonomy, $level, $sep ) { | |
// get all the terms associated with this post | |
$terms = get_the_terms( $id, $taxonomy ); | |
// check if there's terms in general | |
if ( $terms && !is_wp_error( $terms ) ) { | |
// check if level's not zero | |
if ( $level != 0 ) { | |
// build array that only contains terms with correct hierarchical level | |
$level_terms = array_filter( $terms, function ( $term_obj ) use ( &$taxonomy, &$level ) { | |
$ancestors = get_ancestors( $term_obj->term_id, $taxonomy ); | |
if ( count( $ancestors ) == $level ) { | |
return true; | |
} else { | |
return false; | |
} | |
}); | |
// check that it's not empty | |
if ( !empty( $level_terms ) ) { | |
// get last key of array | |
$last_key = array_search( end( $level_terms ), $level_terms ); | |
// build $links string | |
$links = ''; | |
foreach ( $level_terms as $key => $level_term ) { | |
$links .= '<a href="' . get_term_link( $level_term ) . '" rel="tag">' . $level_term->name . '</a>'; | |
if ( !( $key == $last_key ) ) { | |
$links .= $sep; | |
} | |
} | |
echo $links; | |
// else echo that | |
} else { | |
echo 'n/a'; | |
} | |
// else show top level domains | |
} else { | |
// loop trough the terms | |
foreach ( $terms as $key => $term ) { | |
// if term is top level, write in separate array | |
if ( $term->parent == 0 ) { | |
$toplevel_terms[] = $term; | |
} | |
} | |
if ( !empty( $toplevel_terms ) ) { | |
// get the last key in the array (for the separator) | |
$last_key = array_search( end( $toplevel_terms ), $toplevel_terms ); | |
// links string | |
$links = ''; | |
// loop through toplevel array... | |
foreach ( $toplevel_terms as $key => $toplevel_term ) { | |
// and build $links string | |
$links .= '<a href="' . get_term_link( $toplevel_term ) . '" rel="tag">' . $toplevel_term->name . '</a>'; | |
// if not the last term, add separator | |
if ( ! ( $key == $last_key ) ) { | |
$links .= $sep; | |
} | |
} | |
echo $links; | |
} else { | |
echo 'n/a'; | |
} | |
} | |
// if there's no terms at all echo that there's none | |
} else { | |
echo 'n/a'; | |
} | |
} |
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
/** | |
* Template Tag: the_toplevel_terms | |
* | |
* Echo out all the top level terms of an hierarchical taxonomy, | |
* based on id and taxonomy. You should pass a separator. | |
* | |
* @param $id int id of the post | |
* @param $sep string separator in between term links | |
* @param $taxonomy string the taxonomy in question | |
* @return void | |
* @uses get_the_terms() | |
* @uses get_term_link() | |
*/ | |
function prefix_the_toplevel_terms( $id, $taxonomy, $sep ) { | |
// get associated terms based on taxonomy | |
$terms = get_the_terms( $id, $taxonomy ); | |
// loop trough the terms | |
foreach ( $terms as $key => $term ) { | |
// if term is top level, write in separate array | |
if ( $term->parent == 0 ) { | |
$toplevel_terms[] = $term; | |
} | |
} | |
if ( !empty( $toplevel_terms ) ) { | |
// get the last key in the array (for the separator) | |
$last_key = array_search( end( $toplevel_terms ), $toplevel_terms ); | |
// links string | |
$links = ''; | |
// loop through toplevel array... | |
foreach ( $toplevel_terms as $key => $toplevel_term ) { | |
// and build $links string | |
$links .= '<a href="' . get_term_link( $toplevel_term ) . '" rel="tag">' . $toplevel_term->name . '</a>'; | |
// if not the last term, add separator | |
if ( ! ( $key == $last_key ) ) { | |
$links .= $sep; | |
} | |
} | |
echo $links; | |
} else { | |
echo 'n/a'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment