Last active
July 28, 2016 16:06
-
-
Save codekraft-studio/a64e421490294527146b0ed6f12256eb to your computer and use it in GitHub Desktop.
WordPress function to print a back button to the first term ancestor
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
/** | |
* Print a back button to the first term ancestor | |
*/ | |
function wp_term_back_to_parent($id = null, $args = array()) { | |
if( !$id ) { | |
$id = get_query_var('cat'); | |
} | |
// get term ancestors | |
$ancestors = get_ancestors( $id, 'category' ); | |
$direct_parent = $ancestors ? $ancestors[0] : false; | |
if( $direct_parent ) { | |
$elem_id = isset($args['id']) ? ('id="' . $args['id'] . '"') : ''; | |
$title = __('Back to') . ' ' . get_cat_name($direct_parent); | |
printf( | |
'<span %s><a href="%s" rel="prev" title="%s" >%s</a></span>', | |
$elem_id, | |
get_category_link($direct_parent), | |
$title, | |
$title | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment