Skip to content

Instantly share code, notes, and snippets.

@codekraft-studio
Last active July 28, 2016 16:06
Show Gist options
  • Save codekraft-studio/a64e421490294527146b0ed6f12256eb to your computer and use it in GitHub Desktop.
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
/**
* 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