Link to home
Start Free TrialLog in
Avatar of LezlyPrime
LezlyPrimeFlag for United States of America

asked on

Wordpress - navigation sub-menu question

Wordpress - navigation sub-menu question

I'm fairly new to Wordpress but not to PHP. I'm having issues loading my navigation with sub-menus. I do not want to use a plug-in or the dashboard.

Here is the menu structure:

Home
About
Products
      Rings
            Gold
            Silver
      Pendants
      For Men      
Services
      Technical
      Administrative      
Contact Us

Screen shots:

Home, About, & Contact Us pages - these look fine:

User generated image
Products page - messed up:

User generated image
Services page - messed up:

User generated image

Below is the code from Codex that I used.

<?php
$allPages = wp_list_pages();
    if ($allPages) {
        foreach($allPages as $onePage) {
            if(!$post->post_parent){
                // will display the subpages of this top level page
                $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
            } else {
                // diplays only the subpages of parent level
                $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
            }

            if($post->ancestors) {
		// now you can get the the top ID of this page
		// wp is putting the ids DESC, thats why the top level ID is the last one
		$ancestors = end($post->ancestors);
		$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
		// you will always get the whole subpages list
            }

            if ($children) {
                echo '<ul><li>';
        	echo $children;
        	echo '</li></ul>';
            }
        }
    }
?>

Open in new window


I did some testing in the conditional section of code & no matter what I did, the results never changed. So on a whim, I deleted everything except for this line of code:

$allPages = wp_list_pages();

And the results remained the same.

I've looked at tons of other code, on forums & in articles. None of the code presented includes anything like the line above & when I implement any of these solutions into my project, nothing shows in the navigation section. I don't get an error, but there is no navigation menu.

For example, in this codex code from wp_list_pages:

<?php
  if($post->post_parent)
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  else
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  if ($children) { ?>
  <ul>
  <?php echo $children; ?>
  </ul>
  <?php } ?>

Open in new window

 
I get nothing. No errors, but no menu, either.

What am I missing here?

Thank you in advance for any suggestions or help.
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LezlyPrime

ASKER

Because I couldn't get that to work either! ~lol. Here's the code I used in my header template:

<?php wp_nav_menu( array( 'theme_location' => 'mbmMenu' ) ); ?>

...where mbmMenu is the name of my Menu in the dashboard. The output looks exactly the same as the screen shots above.

Here is a screen shot of my menu settings in the dashboard.

User generated image
If it's a CSS issue, then I've seen something about that but didn't read it closely. Thanks.
>> If it's a CSS issue, then I've seen something about that but didn't read it closely. Thanks.

It's definitely a CSS issue.  What's going on in both scenarios (using wp_list_pages() or Custom Menu) is that you don't have the correct CSS to display drop-down items so the submenus display funky.  The home page is probably using wp_list_pages() but only looking for top-level pages and that's why it displays ok.  

Usually this happens because you select an older theme and the theme dev hasn't properly set the CSS for the newer WordPress functionality.  Are you married to the theme?
Just saw an issue with my Jewelry section & moved it all in one level.
Apologies! Been working on a VB project & finally got back to WP. Thanks! The menu system in the theme worked beautifully!