Consider this a complement to toscho’s solution.
I believe that …
$this->alternate = ($this->alternate != 'background_1') ? 'background_1' : 'background_2';
$css_class[] = $this->alternate;
… will do what you want.
The difference is that toscho’s solution, using the static
keyword, will make that variable static for any instantiation of this walker– that is, all instances will share the same value. If you instantiate two walkers the second will pick up where the last left off. That may be what you want, or it may not be. Using $this
will limit it to the one instance of the walker only.
Related Posts:
- $item->url not retrieving url in custom walker?
- How to prevent custom walker from creating sub navigation for pages that are not relatives of the current page?
- How can I hide children of draft pages using wp_list_pages()?
- How to List All Pages (With their template names) Within a Website
- How can I list all pages with their templates?
- Add Parent to List of Subpages
- get all page IDs from wp_list_pages
- Add class to the items in wp_list_pages
- Display grandchildren on child and grandchild pages using wp_list_pages
- Load parent pages when there are no child pages
- create shortcode to show children if any otherwise siblings
- dynamically limit depth of wp_list_pages
- Conditional styling of wp_list_pages bullet points [closed]
- Setting multiple values to as sort_column
- wp_list_pages – Using a Walker to customize output order
- Custom category listings
- How can I include private pages in a page list?
- Do Not Display Parent Page if No Subpages
- Arrange Combined List of Pages and Categories
- Make wp_list_pages print slugs instead of titles
- List pages within a certain parent and show published month
- Change Parent Name with wp_list_pages?
- List all Posts under heading in wp_list_pages menu
- wp_list_pages() but only show children on the branch you are on
- Append a code when at the current page in wp_list_pages()
- WordPress Side Navigation with Parent Heading and Child Sub Pages
- List pages to show only Whitelisted sub pages
- Get Parent List Class for Child Pages Using Walker
- List subpage of subpage
- wp_list_page with something like showpost
- How can I list all page titles and IDs?
- Hide Parent if No Children
- Add Parent to Subpage List
- active parent page when clicked on childpage
- WP List Pages – Add Title Attributes to Anchors
- wp_list_pages Hierarchical Help
- remove auto generated pages from the menu?
- Inset image thumbnail from page into list
- Exclude current page in wp_list_pages
- Sub Navigation in Sidebar
- wp_list_pages bug in “number” parameter
- Targeting specific instane of wp_list_pages
- Renaming wp_list_pages class
- wp_list_pages issue/bug?
- WordPress get_pages sub nav show current page
- Change class name in WordPress wp_list_page nav
- Non page link in wp_list_pages
- only display Pages which have children
- add a.parent in wp_list_pages
- wp_list_pages by taxonomy?
- wp_list_pages Format only on Recently Modified Pages
- Adding custom class names to anchor in wp_list_pages
- Walker Class : extract function and wp_list_pages
- Exclude pages by custom field (with yes/no) storing wrong data?
- What’s the WordPress way to get custom HTML for child pages list (with a second relative link)? Currently running regex over wp_list_pages() output
- Menu items description? Custom Walker for wp_nav_menu()
- Custom Walker: how to get ID in function start_lvl
- Split up wp_nav_menu with custom walker
- Custom Nav walker display current menu item children, or siblings on no children
- Add a custom walker to a menu created in a widget
- Using a menu walker add a custom item at the end of the menu’s items
- Error “Trying to get property of non-object” with Custom Walker for wp_nav_menu
- Mega Menu Walker
- How to count nav menu items?
- How Does The Walker Class Work?
- start_lvl Ignored in Custom walker_nav_menu
- What is a good resource to find out about making custom walkers?
- Does Extending Multiple Nav_Menu_Walkers Allow Nested Menus?
- Add Caret to Menu Items with Sub-Menus in WordPress Theme
- Return parent post with its children using WP_Query?
- Comment Walker vs. Comment Callback
- Custom menu walker: how can i check first item
- How does a minimal menu walker look like?
- Display only page specific sub menu items using Custom Walker
- How do I dynamically populate wp_nav_menu from a custom taxonomy?
- Assign posts to taxonomy terms instead of the taxonomy terms to posts?
- Strict Standards Error bootstrap navwalker
- WordPress Menu Custom Walker Class
- Show just one level of child pages, wp_list_pages woe
- How to create this custom menu walker?
- WordPress 4.4+ breaks Walker Extension
- Call custom field into menu item
- add span class inside wp_nav_menu link anchor tag
- Shortcode producing headers already sent error
- Add class to top level menu item if it has multiple child levels
- Determine if a navigation item has children
- List Hierarchical Term List with Count with Related Term
- Why is my menu order not working with wp_nav_menu?
- Highlighting current item of custom post types’ sub pages, listed by wp_list_pages
- Custom Nav Walker menu – Display category count
- Any walker causes blank menu?
- Extend walker class with custom post types [closed]
- How to hook wp_list_pages?
- How do I pass an argument to my custom walker?
- Admin Panel – Disable Moving Selected Terms To Top of Metabox
- Simple Navigation Walker – Wrapper-class around first sub-menu
- Custom Nav Walker menu – Display children count
- Add Page ID class to nav menu items
- Incrementing a class in a custom walker
- Using string instead of object class instantiation on the walker argument breaks wp_nav_menu