2

I'm trying to add my own field to the menu using get_template_part. The problem is that my code is not working well because it is outside <ul id="primary-menu" class="nav-menu">

here is php:

<?php
$open_g = get_template_part('template/open_google_nav', false);
$items_wrap = '<ul id="%1$s" class="%2$s">%3$s';
$items_wrap .= sprintf( '<li id="item-google-nav">%1$s</li></ul>', $open_g );

    wp_nav_menu(
        array(
            'items_wrap' => $items_wrap,
            'theme_location' => 'desktop-menu',
            'menu_class'     => 'nav-menu',
            'menu_id'        => 'primary-menu',
            )
        );         
?>

Here is generated HTML from PHP

<nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation">
"My template part"  --> Here is my template part
<div class="menu-glowne-menu-container">
    <ul id="primary-menu" class="nav-menu">
        <li id="menu-item-32" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-32"><a href="#"><span class="menu-item-description">Desc</span>Menu 1</a></li>
        <li id="menu-item-31" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31"><a href="#"><span class="menu-item-description">Desc</span>Menu 2</a></li>
        <li id="menu-item-30" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-30"><a href="#"><span class="menu-item-description">Desc</span>Menu 3</a></li>
        <li id="item-google-nav">HERE SHOULD BE MY TEMPLATE PART</li>
    </ul>
</div>
</nav>

What am I doing wrong?

  • 1
    `get_template_part` does not _return_ anything, so your attempt at assigning something to `$open_g` here fails. Instead, the content of the template gets written directly into the output buffer, right in that place where you called the function. That is why you see it appearing _above_ your UL in the generated source code. – CBroe Mar 20 '20 at 11:45

0 Answers0