FORUMS CLOSED DUE TO SPAM. YOU STILL CAN ADD POST!

Display sidebar only if children + only display level 1 children

Feed 8 posts, 3 voices

Avatar
96 posts

I want to display a sidebar only on pages which have children, and only display level 1 children. E.g.:

Fruit
- Apples
-- Braeburn  
-- Granny Smith  
- Oranges  
- Plums  

Each of those pages would have a sidebar containing:

- Apples
- Oranges
- Plums

And ‘Contact Us’ (which has no children) would have no sidebar.

I’d like this taken care of automatically when new pages are created.

Any suggestions, please?

 
Avatar
1493 posts

Hi David —

Just for clarity’s sake, “Apples”, “Oranges” and “Plums” are all level 2 in Frog, since “Fruit” is level 1, and only [Root] is level 0.

Is it only level 2 you’re after? (i.e., they will always be child pages to child-of-root pages)

 
Avatar
96 posts

Hi David,

Ah, yes – that’s (of course! ;)) what I mean – only display level 2 children. So:

Home (0)
About Us (1)
Fruit (1)
- Apples (2)
-- Braeburn (3) 
-- Granny Smith (3)
- Oranges (2)
- Plums (2)
Contact Us (1)
 
Avatar
1493 posts

It is possible that this will do the job for you. It’s a mashup of some find-level-1-pages code, and the sidemenu code that crusnac was just looking for:

<?php foreach($this->find('/')->children() as $topPage): ?>
<?php if (in_array($topPage->slug, explode('/', $this->url))) { $thisPage = $topPage->slug; } ?>
<?php endforeach; ?>

<ul>
<?php
   if (count($this->find($thisPage)->children()) > 0 && $this->level > 0) {
     foreach($this->find($thisPage)->children() as $menu): ?>
       <li<?php echo (url_start_with($menu->url)) ? ' class="current"': null; ?>><?php echo $menu->link(); ?></li>
     <?php endforeach;
   }
?>
</ul>

It’s a bit of a mess :) but I guess the thing to do is put it in a snippet, then call the snippet in the layout (it needs to be in the layout to work). It gives you a class="current" on the <li> tag when you’re anywhwere in the tree belonging to “Apples”, for example. (You’ll see what I mean!)

Hope that helps.

 
Avatar
96 posts

Thanks, David, but something’s amiss.

All I’m seeing is ‘<ul></ul>’ in the HTML and that appears on every page.

 
Avatar
1493 posts

Hmm… That means the conditions are failing for some reason. (Doh!) Is the site live? or localhost development?

Are you up for a quick online chat about this? If so, shoot me a note at madebyfrog at gmail etc.

 
Avatar
96 posts

I was just about to suggest that. :) Email on the way.

 
Avatar
35 posts

funny, i was just working on a similar problem, but trying to figure it in another direction…. see my post here, oh and maybe you can fix what’s wrong with it? ;-)

http://forum.madebyfrog.com/topic/1377?page=1#post_8922

 
Avatar
1493 posts

That means the conditions are failing for some reason.

Yes, and this was the failing code:

... && $this->level > 0)

Oddly enough, this works fine in 0.9.4 (where I had tested the menu code on a localhost install). It turns out that in 0.9.5, the format for this must be:

... && $this->level() > 0)

with the ‘()’ parentheses following “level”, otherwise it fails to report a value. Very strange. Something must have changed! But something to be aware of when using $this->level() in 0.9.5!