Created
March 16, 2021 16:09
-
-
Save interplaydesign/ff77bc7ea5c97083315925b8e655c7ae to your computer and use it in GitHub Desktop.
wp_nav_menu_objects filter
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
| /** | |
| * Menu customizations | |
| */ | |
| add_filter('wp_nav_menu_objects', 'intro_nav_menu_objects', 10, 2); | |
| function intro_nav_menu_objects( $items, $args ) { | |
| //If our menu has the id primary-menu in the wp_nav_menu in our header.php | |
| if ( $args->menu_id == 'primary-menu' ){ | |
| //var_dump( $items ); | |
| //For each item in the menu. the $item->ID is the menu item ID not the page/post ID | |
| //The post id will be the $item->object_id if it is a post/page | |
| $objects_with_featured_images = array( 'post', 'page', 'custom_post_type' ); | |
| foreach( $items as &$item ) { | |
| //Post meta are fields created with advanced custom fields that are targeting menu items | |
| if ( in_array( $item->object, $objects_with_featured_images ) ){ | |
| $post_id = $item->object_id; | |
| $img_url = '';//get_the_post_thumbnail( $post_id ); | |
| $item->title .= $img_url ? '<span class="image">' . $img_url . '</span>' : ''; | |
| } | |
| //Other things you can do | |
| //If its a child menu item | |
| ////if ( $item->menu_item_parent ){ } | |
| //If its a parent menu item | |
| ////if ( in_array( 'menu-item-has-children', $item->classes ) ){ } | |
| } | |
| } | |
| // return | |
| return $items; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment