FinkAvenue FinkAvenue - 7 months ago 30
PHP Question

WordPress get data into variables

I am having trouble figuring out how to get data instead of printing it. I want to get the data and store it into variables.

I want to get the navbar instead of printing it, how do I get it so I can store it in the variable

$navbar
?

wp_nav_menu( array('menu_class' => 'nav navbar-nav navbar-right','theme_location' => 'primary','container' => false,) )

Answer

Can you try below code:

<?php
    $navbar = '';
    ob_start();
        wp_nav_menu( array('menu_class' => 'nav navbar-nav navbar-right','theme_location' => 'primary','container' => false) );
    $navbar = ob_get_contents();  
    ob_end_clean();
    echo $navbar;
?>
Comments