I am trying to fetch items from currently assigned menu. Wordpress documentation says wp_get_nav_menu_items($menu) returns array of menu items. Just to test have a fetched proper menu items I wanted some kind of output. Coming from JS I tried to console log it like this:

$menu = wp_get_nav_menu_items(3); foreach ($menu as $menuItems) { ?> <script> console.log(<?php echo $menuItems?>); </script> <?php }

Afterwards I get error on line where I'm console logging $menuItems:

"Object of class WP_Post could not be converted to string"

Is there any other way to log on frontend or to see the contents of $menu variable?

I also tried to echo $menu directly but not working.

Thanks in advance for any help.

  • 2
    $menuItems is an array of objects. You can't echo objects. – Jacob Peattie Feb 13 '18 at 23:38
  • I just figured it out meanwhile. Small problem actually. Thanks @JacobPeattie on confirmation. – Usce Feb 13 '18 at 23:40

I've figured that using print_r will output array via console.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.