Created
April 25, 2012 15:15
-
-
Save diggy/2490532 to your computer and use it in GitHub Desktop.
Add taxonomy navigation to the WordPress admin toolbar
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
<?php | |
add_action( 'admin_bar_menu', 'pjh_add_taxonomies_to_toolbar', 999 ); | |
function pjh_add_taxonomies_to_toolbar( $wp_admin_bar ) | |
{ | |
// check permissions | |
if( ! current_user_can( 'manage_categories' ) ) return; | |
global $wp_taxonomies; | |
// for each taxonomy | |
foreach( $wp_taxonomies as $taxonomy => $tax_data ) | |
{ | |
// skip non-hierarchical taxonomies | |
if( ! is_taxonomy_hierarchical( $taxonomy ) ) continue; | |
// skip specific taxonomies | |
// if( in_array( $taxonomy, array( 'genre', 'country' ) ) ) continue; | |
$tax = get_taxonomy( $taxonomy ); | |
$url = admin_url( '/edit-tags.php?taxonomy=' . $tax->name ); | |
// get terms | |
$tax_terms = get_terms( $taxonomy, 'number=9999&orderby=count&order=DESC&hide_empty=1' ); | |
// limit number of terms added to toolbar | |
// $tax_terms = array_slice( $tax_terms, 0, 10 ); | |
// add parent nodes to toolbar | |
$wp_admin_bar->add_node( array( | |
'id' => $tax->name, | |
'title' => $tax->labels->name, | |
'href' => $url, | |
'meta' => array( | |
'class' => $tax->name . '-toolbar-page', | |
'title' => $tax->labels->name | |
), | |
'parent' => false | |
) ); | |
// for each term in taxonomy | |
foreach( $tax_terms as $array => $data ) | |
{ | |
// determine parent and set up variables | |
if( $data->parent != 0 ) : | |
$term = get_term( $data->term_id, $taxonomy ); | |
$parent_term = get_term( $data->parent, $taxonomy ); | |
$parent = $parent_term->name; | |
$link = get_term_link( $term ); | |
else : | |
$parent = $tax->name; | |
$term = get_term( $data->term_id, $tax->name ); | |
$link = get_term_link( $term ); | |
endif; | |
$name = esc_attr( $data->name ); | |
$count = $data->count; | |
// add additional query arguments | |
// $query_arg = array( 'foo' => 'bar', 'baz' => 'qux' ); | |
// add subitems to parents | |
$wp_admin_bar->add_node( array( | |
'id' => $name, | |
'title' => $name . ' (' . $count . ')', | |
'href' => $link, | |
//'href' => add_query_arg( $query_arg, untrailingslashit( $link ) ), // add additional query arguments | |
'meta' => array( | |
'class' => $name . '-toolbar-page', | |
'title' => $data->description | |
), | |
'parent' => $parent | |
) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Paste the snippet in the functions.php file of your theme and adjust to your liking.
Notes