Lightning Rank - Private Blog Network

  • Home
  • Done 4 You PBN Service
  • Use My Premium PBN
  • Other Services
    • Amazon Associate Audit
    • Editorial Links
    • Buy Expired Domains
  • Contact
    • Affiliates

Add .first & .last CSS class to WordPress Navigation Menus

November 20, 2015 By Jon Gillham Leave a Comment

When designing a WordPress theme you may want to add some .first and .last css classes to your WordPress navigation menus for styling purposes. This tutorial will show you how to acheive that by placing some filter code into your themes WordPress functions.php file that will modify all your menu items through “wp_nav_menu_objects” by adding .first and .last CSS classes allowing you to style menu items easily.

Step 1: Adding The Menus Modification WordPress Filter

Firstly you will want to open your current themes functions.php file then copy the code block below and paste it into your functions.php file at the bottom, close and save your functions.php file.

//------------- [ Add First & Last CSS Classes To Menu Output ] --------------//

function add_first_and_last($items) {
	$items[1]->classes[] = 'first-menu-item';
	$items[count($items)]->classes[] = 'last-menu-item';
	return $items;
}

add_filter('wp_nav_menu_objects', 'add_first_and_last');

The code above will now automatically add the CSS class .first-menu-item to your menus first navigation item and also add the CSS class .last-menu-item to your menus last navigation item.

You can now easily style and use those css properties in your WordPress themes stylesheet, this is a technique I tend to use in all my themes for easy styling just in case it’s required during develpment.

Filed Under: Wordpress Tutorials Tagged With: css classes, navigation menus, wordpress filter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Services

[wp_cycle]

Recent Posts

  • What It takes to get to #1 in Google
  • Private Blog Network Building Service in 2016 – Is It Still Worth The Risk?
  • UpThemes Scale Back On Theme Options Within Their WordPress Themes
  • Create A WordPress Login Form Shortcode For Your Blog
  • WooThemes Back Online After Malicious Hack & Further DDoS Attack

Our Team

Jon Gillham

Father, Husband and mechanical engineer. Jon has the easy job he builds and tests systems that Kelley, Hmd and their teams execute!
Ahmed Ali

Ahmed is the man that makes sure everything runs smoothly. A true jack of all trades he makes sure each site is built to our high standards!

Quick Contact

Checking...

Ouch! There was a server error.
Retry »

Sending message...

Copyright © 2021 . Experts at Harnessing the Power of Expired Domains to Help You Outrank Your Competition