0
votes

im trying to put icons in front of every menu item with ACF. I used this tutorial here https://www.advancedcustomfields.com/resources/adding-fields-menu-items/

It works perfectly but it always add icon AFTER the menu item but I need it to put it IN FRONT of it. I believe it is just some minor code edit I cant see.

Here is my code:

add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2);

function my_wp_nav_menu_objects( $items, $args ) {
  
  // loop
  foreach( $items as &$item ) {
    
    // vars
    $icon = get_field('icon', $item);
    
    
    // append icon
    if( $icon ) {
      
      $item->title .= ''.$icon.'';
      
    }
    
  }
  
  
  // return
  return $items;
  
}

Thank you for any help. ms

1

1 Answers

1
votes

what you do there is that you add it after the title,

so insted of: $item->title .'' . $icon . '';

It should look like this: $item->title = $icon . $item->title;