1

I'm trying to add a custom PHP script inside of the nav menu (before the closing ) and struggling.

Code here:

<?php
      wp_nav_menu(array(
                        'theme_location' => 'menu-1',
                        'menu_id' => 'primary-menu',
                        'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s<div class="my-div">' . $addCats . '</div></ul>'
                    ));
      $addCats = require_once('custom_menu_addon.php');
?>

The above code includes the file outside of the new added to the menu:

enter image description here

Any recommendations for getting this to print inside the div? Am I using the wrong command to pull the file contents there?

Thank you in advance for your help and guidance! <3

4
  • 1
    You can't have a <div> as a direct child of an <ul>, that's invalid HTML. Commented Jan 18, 2019 at 5:40
  • I'm aware, it's going to be further list items but at the moment im just testing what I can print. I was concerned I may have been printing closing tags and whats why it was breaking and therefore tried <div>s!.. Commented Jan 18, 2019 at 7:08
  • What’s even in the PHP file? HTML? Commented Jan 18, 2019 at 7:20
  • PHP, its a cool script we made to fetch categories etc. using it as a dropdown for the final item of the menu. Edited the answer below, seems to work well. Thanks guys! <3 Commented Jan 18, 2019 at 8:35

1 Answer 1

-1

Please Use Below code

<?php
$addcats = array();
ob_start();
require 'custom_menu_addon.php';
$addCats = ob_get_clean();
wp_nav_menu(array(
    'theme_location' => 'menu-1',
    'menu_id' => 'primary-menu',
    'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s<div class="my-div">' . $addCats . '</div></ul>'
));

I've edited this answer after some research into the usage of this object array and put it in context with my code, works well on the site :)

2
  • 1
    Please edit your answer, and add an explanation: why could that solve the problem? Commented Jan 18, 2019 at 7:50
  • As above, I've edited to relate to the question. Good answer but more context would definitely help, I asked because i do not know PHP so well yet - putting it in context was a struggle but @Er was on the right path indeed :) Commented Jan 18, 2019 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.