Jump to content

Sidebar Navigation


bhammeraz
 Share

Recommended Posts

I'm trying to convert a site to this CMS system.  The site uses a Sidebar Navigation menu that is the same on all pages.  I have it running on the Parent page, but it won't show up on any children. 

Here is the code that is on my Parent page.

<div id="sidebar">

<?php

echo "<ul id='subnav' class='nav'>";

foreach($page->children as $child)

  echo "<li><a href='{$child->url}'>{$child->title}</a></li>";

?>

</div>

Thank you!

Link to comment
Share on other sites

Hi Bhammeraz,

welcome to forums.

As I understand it, you want to show the very same navigation [with children of root] on every page. therefore you can't use $page->children(), because in your templates, $page refers to page that's currently opened [therefore children() refers to different pages everytime]

you want rather use something like this:

<div id="sidebar">

   <?php
      echo "<ul id='subnav' class='nav'>";
      foreach($pages->get('/')->children as $child)
         echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
   ?>

</div>

This first selects root page as reference and then iterates through it's children [i.e. first level pages on your site]

Adam

Link to comment
Share on other sites

Couldn't you also use the code in the basic PW installation site where the sub nav gets generated by:

<?php

			// Output subnavigation
			// 
			// Below we check to see that we're not on the homepage, and that 
			// there are at least one or more pages in this section.
			// 
			// Note $page->rootParent is always the top level section the page is in, 
			// or to word differently: the first parent page that isn't the homepage.

			if($page->path != '/' && $page->rootParent->numChildren > 0) { 

				// We have determined that we're not on the homepage
				// and that this section has child pages, so make navigation: 

				echo "<ul id='subnav' class='nav'>";

				foreach($page->rootParent->children as $child) {
					$class = $page === $child ? " class='on'" : '';
					echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";	
				}

				echo "</ul>";
			}

			?>

So in this case it's checking if the section has sub-children rather than the page itself (plus there's an extra check in there for homepage that you don't really need I think. Anyway, hope that makes sense...

  • Like 1
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...