简体   繁体   English

在 wp_nav_menu ul 中添加自定义语言选择器作为 li

[英]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().我一直在尝试将 li 中的自定义语言选择器添加到我的 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).我想将它添加到“items_wrap”中,但是当我这样做时,它给了我一个 php 语法错误(我将添加一个语法错误的图像)。 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.我尝试将 ' 围绕 template_url 替换为 " 但随后它仅在我的网站上显示为文本。此外,正如它在我的网站上显示的那样,另一个问题是我的自定义语言选择器首先出现在我的导航中,但我希望它可以是菜单上的最后一个 li 项目。这一切可能有点令人困惑,因为我从来没有像这样自己制作自定义语言选择器,所以可能有更好的解决方案。我也尝试将我的自定义语言选择器从 php 标签中取出来items_wrap 但问题是它在 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>',
            
            
        ) );
    
        ?>

语法错误

You're closing your string and opening it, every time you add <?php bloginfo('template_url'); ?>每次添加<?php bloginfo('template_url'); ?>时,您都要关闭字符串并打开它<?php bloginfo('template_url'); ?> <?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所以在你的情况下,你需要通过items_wrap元素并执行以下操作

'<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 .如您所见,我正在使用' using 关闭字符串. to concatenate, adding the function bloginfo('template_url') concatenating again and then finishing the string.连接,添加函数bloginfo('template_url')再次连接,然后完成字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM