K
K
Konstantin Shulyarenko2020-12-07 00:11:21
PHP
Konstantin Shulyarenko, 2020-12-07 00:11:21

How to display wp_nav_menu inside HTML structure via shortcode?

Hello, I am making a plugin in which I collect a custom menu using the Walker class, then display it using the wp_nav_menu function through a shortcode. The task is to display the menu along with the following HTML structure:

<div id="main">
   <div class="wrap1">
     <div class="wrap2">
       <div class="wrap3">
         ТУТ САМО МЕНЮ
       </div>
     </div>
     <div class="arrows">
     тут некоторые теги управления
     </div>
  </div>
</div>

I tried to display in the shortcode just like this:

function my_function(){
        $variable = '<div id="main"><div class="wrap1"><div class="wrap2"><div class="wrap3">';
        $variable .= wp_nav_menu( 'тут массив аргументов меню' );
        $variable .= '</div></div><div class="arrows"><i>некоторые теги управления</i></div></div></div>';  
    
         return $variable;
    }
    add_shortcode('menu-test','my_function');

However, as a result, everything is displayed separately, first the menu, and then the entire HTML structure
. Tell me how to display wp_nav_menu inside the HTML structure?

Thank you for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Morev, 2020-12-07
@SeaInside

wp_nav_menu(['echo' => false]), m? :)
It displays it by default, but does not return it, so the menu is displayed first, then everything else.

K
Konstantin Shulyarenko, 2020-12-07
@web86

Wow, exactly!
I had ['echo' => true], changed it to false and everything came out fine.
Thank you!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question