【问题标题】:Adding a custom language picker inside wp_nav_menu ul as li在 wp_nav_menu ul 中添加自定义语言选择器作为 li
【发布时间】:2021-02-11 10:33:56
【问题描述】:

我一直在尝试将 li 中的自定义语言选择器添加到我的 wp_nav_menu() 中。我想将它添加到“items_wrap”中,但是当我这样做时,它给了我一个 php 语法错误(我将添加一个语法错误的图像)。我尝试将 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>',
            
            
        ) );
    
        ?>

【问题讨论】:

    标签: php wordpress bootstrap-4 nav


    【解决方案1】:

    每次添加&lt;?php bloginfo('template_url'); ?&gt;时,您都会关闭并打开字符串

    您需要使用concatenation 将这些值添加到您的字符串中。

    因此,在您的情况下,您需要通过 items_wrap 元素并执行以下操作

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

    如您所见,我用' 关闭字符串,使用. 连接,添加函数bloginfo('template_url') 再次连接,然后完成字符串。

    【讨论】:

    • 好的,谢谢!此解决方案有效。我还将 bloginfo('template_url') 替换为 get_template_directory_uri() 然后它实际上将图像而不是图像的路径显示为文本。此外,如果您希望语言选择器成为 li 的结尾而不是开头,则必须在第一个 li 标记之前放置 %3$s。
    猜你喜欢
    • 2012-11-18
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多