Search code examples
phpwordpressbootstrap-4nav

Adding a custom language picker inside wp_nav_menu ul as li


I have been trying to add my custom language picker which is inside li to my wp_nav_menu(). I want to add it inside 'items_wrap' but as I do it it gives me a php syntax error(I'll add a image of the syntax error). I've tried to replace ' around template_url with " but then it just shows as a text on my website. Also as it shows on my website another problem is that my custom language picker somes first in my navigation but I wish it could be the last li item on the menu. This all may be a little bit confusing as I have never made custom language picker myself like this so there may be better solutions. I've also tried to but my custom language picker out of php tag and out of items_wrap but then the problem is that it is outside the ul.

 <?php
        wp_nav_menu( array(
            'theme_location' => 'primary', // Defined when registering the menu
            'menu_id'        => 'primary-menu',
            'container'      => false,
            'depth'          => 2,
            'menu_class'     => 'navbar-nav ml-auto',
            'walker'         => new Bootstrap_NavWalker(), // This controls the display of the Bootstrap Navbar
            'fallback_cb'    => 'Bootstrap_NavWalker::fallback', // For menu fallback
            'items_wrap' => '<ul id="%1$s" class="%2$s"><li class="language nav-item dropdown">
                            <a class="nav-link dropdown" href="" id="dropdown09" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><img src="<?php bloginfo('template_url'); ?>/img/eng.png">ENG<img src="<?php bloginfo('template_url'); ?>/img/downarrow.png"></a>
                            <div class="dropdown-menu" aria-labelledby="dropdown09">
                                <a class="dropdown-item" href=""><img src="<?php bloginfo('template_url'); ?>/img/eng.jpg">  EN</a>
                               <a class="dropdown-item" href=""><img src="<?php bloginfo('template_url'); ?>/img/rus.png">  RU</a>
                               
                            
                            </div>
                        </li>%3$s</ul>',
            
            
        ) );
    
        ?>

syntax error


Solution

  • You're closing your string and opening it, every time you add <?php bloginfo('template_url'); ?>

    You need to use concatenation to add those values to your string.

    So in your case, you need to go through the items_wrap element and do the following

    '<a class="dropdown-item" href=""><img src="' . bloginfo('template_url') . '/img/rus.png">RU</a>'
    

    As you can see, I'm closing the string with ' using . to concatenate, adding the function bloginfo('template_url') concatenating again and then finishing the string.