A better and simpler approach:
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');
Related Posts:
- wp nav menu: add attributes to menu items [duplicate]
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- Menu Custom Data Attributes
- How to modify mobile nav menu text in theme
- Extend Menu Walker Output
- conditional: if is page, and all subpages
- wp nav menu: show submenu below li item
- Styling an “active” link outside of WordPress default menu
- how to changes mobile menu toggle breakpoint in WordPress
- How to make only the valid active page of a multilevel menu active with – “.nav li.current-menu-item a”?
- dynamic php menu with hidden sub-categories only showing when the category name is clicked
- How to let mobile navigation menu close when link is clicked?
- Bring Font Awesome icons inside menu A tags
- Display specific main Sub Nav on Woocommerce product pages
- How limit the width of a fixed menu-bar on scroll?
- WP_Nav Highlight Parent Issue
- How to change the menu color
- Mega Menu Walker
- Menu Items Disappearing
- WordPress Shortcode and Dynamic CSS
- How to Use the Function is_user_logged_in To Display Different Menus?
- Replacing a specific menu item
- How can I add a class to a single menu item?
- WordPress loop specific thumbnail size
- How can I add a custom class to only nav sub-menu > li > a items in WordPress?
- Style wp_page_menu() items
- WordPress override bloginfo variable
- An unwanted inline style is added to my body tag
- Get different images for mobile and desktop with php, advanced custom fields without using js and ajax
- How to stop featured image thumbnail [set as background image] on blog index page just repeating same image across all posts
- Multiple WordPress Menus that will only display all pages
- WP Walker using custom css classes
- Change list item content in menu navigation to add a child element with specific class
- Selectbox in admin panel function linking to CSS
- Using if/else statements with output from theme options
- Is there CSS to expand the gallery limit on Index Pages?
- How to display pictures from database?
- Child page menu in sidebar
- Title Case WordPress Menu Items
- Different post styles depending on category
- Trying to store submenu items to render out after main menu
- How to change menu order item
- custom css for admin only
- responsive.css in the WordPress should be prioritized
- WordPress style.css not updating, only after theme reload
- Adding logo to WordPress header
- Thumbnails appear to be zoomed
- Odd / Even posts add class minus first post
- Two menus show up
- WordPress archive page showing 404 Error [duplicate]
- Parsing Menu Items and Blog Posts
- Removing WordPress Footer -without access to PHP code?
- CSS change in woo commerce Place Order Text [closed]
- Post repeated when opening in overlay, how do I solve this? [closed]
- How to display different submenus?
- Get the name of menu item with wp_nav_menu
- Split site title and apply different classes
- Twentyfifteen style author comment
- Dynamic CSS through PHP issue
- Why can’t I add a custom image in my navigation?
- How to make changes to style.php [closed]
- Remove the Tag from wp_nav_menu
- Horizontal drop down category with hover effect working
- Create Columns with Custom Walker and CSS Classes
- Adding an Anchor Link to a wordpress menu using WPBakery Page Builder
- Why would the child theme load in the Customize preview, but not on the site itself?
- echo cutom css code to WordPress page template file ? is this safe?
- I can’t go to the page archive-{post_type}.php
- Problem with displaying CSS Stylesheets – Am I adding them correctly in my wordpress child theme?
- Trying to generate a CSS file in wordpress uploads folder
- WP Website is shown in HTML
- How to put 2 php codes in functions.php without site crashing
- I got big white screen when i attach main style sheet of Html in functions.php
- wp_get_nav_menu_items() with ACF
- How to switch between two primary menus (programatically0
- Used a code to revoke some dashboard menus for my contributors but for some reason it’s revoked me access to the editor [closed]
- Add a default style if no Featured image is selected
- How to give custom roles the capability to edit one Menu instead of every Menu
- Primary navigation menu with timed items from custom post type
- Styling a specific post after hovering over it
- custom field – changing an element or background of id div – different versions not working
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- CSS loading as empty file in Custom Theme
- Where to find the html for WordPress site? [closed]
- Two theme locations for two menus, but only one is showing up
- How to remove image on single product and get product to span page?
- widget: input the px value from user and use it as inline style in widget function
- Contact form field in wordpress menu
- How to call a certain object/menubar in a PHP file
- Remove all nav menu classes except those which begin with certain letters
- How Can I Center the Logo in the Navigation Menu? [closed]
- Nav Menu Display
- Adding a unique class to wp_nav_menu
- Converting Menu Links to List Elements
- How to get shared CSS body class between translated WPML pages [closed]
- How to create and add js and css file when server is not on my PC?
- which php or css file has generated an inline style in WordPress that overwrites my css
- inner-wrap div pushing custom table far down on page
- Custom nav with bootstrap dropdown
- Styling best practices for single pages/templates