Support

Account

Home Forums Front-end Issues Display a new field in menu items

Helping

Display a new field in menu items

  • Hi,

    I’m trying to put a second line of text in menu items on the main menu of wordpress. I added a new field in ACF Field Group and it works. I added the text in the backend of the menu and I added the code that I found in ACF documentation but… Nothing changed. Maybe there is some kind of error but I can’t figure out where it could be.

    Can someone help me to find out how to solve this?

    Thnaks guys,
    Davide

    PS: I post here some screen and my code in order to help you on understading what I’ve done until now

    *** Screen of ACF Field Group ***
    Click me

    *** functions.php code ***

    //Navbar from Bootstrap 4 to WordPress
    require_once get_template_directory() . '/wp-bootstrap-navwalker.php';
    
    //Add the subtitle field with Advanced Custom Fields
    add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2);
    
    function my_wp_nav_menu_objects( $items, $args ) {
    	
    	foreach( $items as &$item ) {
    		
    		$menu_navbar_sottotitolo = get_field('menu_navbar_sottotitolo', $item);
    		
    		if( $icon ) {
    			$item->title .= '<br/><small>'.$menu_navbar_sottotitolo.'</small>';
    		}
    	}
    	
    	return $items;
    }

    *** header.php code ***

    <header class="site-header" role="banner">
    	<div id="mainNav" class="navbar navbar-expand-lg fixed-top unselectable">
    		<div class="container">
    			<a class="navbar-brand" href="index.html">
    				JustWeb Factory
    			</a>
    			<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarContent" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
    				Menu
    				<i class="fa fa-bars"></i>
    			</button>
    
    			<?php
    				wp_nav_menu( array(
    					'menu'              => 'primary',
    					'theme_location'    => 'primary',
    					'depth'             => 2,
    					'container'         => 'div',
    					'container_class'   => 'collapse navbar-collapse',
    					'container_id'      => 'navbarContent',
    					'menu_class'        => 'navbar-nav ml-auto',
    					'fallback_cb'       => 'WP_Bootstrap_Navwalker::fallback',
    					'walker'            => new WP_Bootstrap_Navwalker())
    				);
    			?>
    
    		</div>
    	</div>
    </header>

    *** Actual navbar ***
    Click me

    *** Desired navbar ***
    Click me

  • You could do this without ACF using the Description field that’s built in.

    BUT, if you really want to use ACF I think the issue is your if ($icon) — it should be if ($menu_navbar_sottotitolo) because that’s the variable you set in the line above.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Display a new field in menu items’ is closed to new replies.