WordPress.org

Ready to get started?Download WordPress

Forums

Add a dynamic menu of parent pages and sub pages... (2 posts)

  1. calverte
    Member
    Posted 1 year ago #

    While working on a template to wordpress theme conversion, which used the Dynamic Drive's Smooth Menu. I needed to dynamic build my menu. Here is what I came up with as far as the menu output:

    <div id="smoothmenu1" class="ddsmoothmenu">
    <ul>
    <li><a href="<?php echo get_option('home'); ?>">Home</a></li>
    
    <?php
    
    $pages = get_pages('parent=0'); 
    
    foreach ( $pages as $page ) {
    
         $children = get_pages('child_of='.$page->ID.'&sort_column=post_title');
         $option = '<li><a href="' . get_page_link( $page->ID ) . '">' .$page->post_title.'</a>';
    
    	if ($children) {
    	     $option .= '<ul>';
    
                 foreach ( $children as $child ) {
    		$option .= '<li><a href="' . get_page_link( $child->ID ) . '">' .$child->post_title.'</a></li>';
    		}
    
    	     $option .= '</ul>';
    
    	}
    
    	$option .= '</li>';
            echo $option;
    
    }
    ?>
    
     </ul>
     <br style="clear: left" />
     </div>
  2. haxxxton
    Member
    Posted 1 year ago #

    i would advise using the wordpress dashboard menu's section.. which allows for nested menus and far more control over what is and isnt displayed..

    add this to your functions.php
    register_nav_menu( 'primary', 'header' );
    REF: HERE

    you should then see a link under Appearance for your theme that is called 'Menus'.. you should then be able to create menus of the pages you want for your theme..

    all you would need to do is change the 'container' variable when you call the wp_nav_menu() call in your header to 'ddsmoothmenu'
    REF: HERE

Topic Closed

This topic has been closed to new replies.

About this Topic