Jump to content

Submenu with "overview" links


thetuningspoon
 Share

Recommended Posts

I've put together some php (with the help of you guys on the forum) to create a hierarchical list menu. It uses a function which it calls recursively to create the drop-downs/submenus. It works great, but now my client has thrown a wrench in the works: they want the first link on all (or most) of the submenus to be an "overview" link which actually just directs you to the parent link, in case people don't realize they can click on the top level link itself.

I'm having trouble figuring out how to do this with my current php, but I'm also wondering if there might be a better way to do it altogether: by creating the overview as its own "page" in the site structure. If I do this, what's the best way to get that page to simply redirect you to a different page? PHP header redirect in the template file?

Thanks for your help. The code I'm using for the menu is below.

<?php	
//Choose the parent page you want the menu to be based on	 
$parentPage = $pages->get("/");

//Set a limit for the number of top level menu links
$children = $parentPage->children("limit=6");
//Include the parent page as the first item in the menu
$children->prepend($pages->get("/"));

//Call the function to build the menu tree
listChildrenTree($children);

function listChildrenTree($children) {
?>

 <ul>
 <?php
 //Loop through each of the child pages
 foreach($children as $page) {

  //If this is the current page, create a CSS class for it
  $class = $page->id == wire('page')->rootParent->id ? "class='on'" : '';

  //If this is the homepage, use the following image
  $homeImg = '';
  if($page->id == wire('pages')->get("/")->id) {
   $homeImg = '<img src="' . wire('config')->urls->templates . 'styles/images/home.png" width="24" height="28" alt="" />';
  }

  //Markup time!
  ?>

  <li>
   <a href="<?php echo $page->url ?>" <?php echo $class ?>>
 <?php echo $homeImg ?> <?php echo $page->title ?>
   </a>

   <?php
   //If the current page has children and it is not the homepage, call the function recursively to create a submenu
   if($page->numChildren && $page->id != wire('pages')->get("/")->id) {
 listChildrenTree($page->children);
   } ?>
  </li>

 <?php
 } ?>
 </ul>
<?php
} ?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...