Skip to content

Instantly share code, notes, and snippets.

@arelthia
Last active June 9, 2017 18:34
Show Gist options
  • Save arelthia/9a146222fb3df32b9b2709615113b6b0 to your computer and use it in GitHub Desktop.
Save arelthia/9a146222fb3df32b9b2709615113b6b0 to your computer and use it in GitHub Desktop.
Add custom classes to WordPress menus
// Add extra classes for the first and last items in all WordPress menus
add_filter( 'wp_nav_menu_objects', function ( $items ) {
if ( ! empty( $items ) ) {
$items[1]->classes[] = 'menu-item-first';
$items[ count( $items ) ]->classes[] = 'menu-item-last';
}
return $items;
} );
<?php
/**
* Add a custom link to the end of a specific menu that uses the wp_nav_menu() function
*/
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
if( $args->theme_location == 'footer_menu' ){
$items .= '<li><a title="Admin" href="'. esc_url( admin_url() ) .'">' . __( 'Admin' ) . '</a></li>';
}
return $items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment