Topic: Drop Down Menu

I need some help to modify the following menu code:

<ul id="nav">
      <li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">Home</a></li>
<?php foreach($this->find('/')->children() as $menu): ?>
      <li class="drop">
         <?php echo '<a href="#"'.(in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null).'> <span>'.$menu->title().'</span></a>'; ?>
        <ul>
        <?php foreach($menu->children() as $child):?>
            <li><?php echo $child->link() ?></li>
        <?php endforeach; ?>
        </ul>
      </li>
<?php endforeach; ?> 
</ul>

I need the class="drop" only to be displayed if there is a child.

Thumbs up

2

Re: Drop Down Menu

I think all you needed to do was move it from where it was (children) to the next node down (child)

<ul id="nav">
      <li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">Home</a></li>
<?php foreach($this->find('/')->children() as $menu): ?>
      <li>
         <?php echo '<a href="#"'.(in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null).'> <span>'.$menu->title().'</span></a>'; ?>
        <ul>
        <?php foreach($menu->children() as $child):?>
            <li class="drop"><?php echo $child->link() ?></li>
        <?php endforeach; ?>
        </ul>
      </li>
<?php endforeach; ?> 
</ul>

Last edited by yello (2010-04-19 20:35)

Re: Drop Down Menu

This does not solve my problem.  I need the class="drop" to be displayed only if there are children.  I don't want it to be display if there are no children. 

Currently the class="drop" is displayed for every menu item except the home page.

Thumbs up

4

Re: Drop Down Menu

I think the code above is currently printing empty UL lists...whether you are intending to do something there i dont know but back to your problem...

How about adding an if like below

if ($menu->children()) { echo 'class="drop"'; }

Last edited by yello (2010-04-19 21:33)

Re: Drop Down Menu

That solved the problem. 

Here is the final code in case anyone esle needs it:

<ul id="nav">
      <li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">Home</a></li>
<?php foreach($this->find('/')->children() as $menu): ?>
      <li <?php if ($menu->children()) { echo 'class="drop"'; } ?>>
         <?php echo '<a href="#"'.(in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null).'> <span>'.$menu->title().'</span></a>'; ?>
        <ul>
        <?php foreach($menu->children() as $child):?>
            <li><?php echo $child->link() ?></li>
        <?php endforeach; ?>
        </ul>
      </li>
<?php endforeach; ?> 
</ul>

Thumbs up

6

Re: Drop Down Menu

Glad you're sorted.

Re: Drop Down Menu

Is there a way to stop it from creating the un-needed <ul> ?

"When you want something, all the universe conspires in helping you to achieve it." - The Alchemist

8

Re: Drop Down Menu

Does this do the trick?

<ul id="nav">
      <li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">Home</a></li>
<?php foreach($this->find('/')->children() as $menu): ?>
      <li<?php if ($menu->children()) { echo ' class="drop"'; } ?>>
         <?php echo '<a href="#"'.(in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null).'> <span>'.$menu->title().'</span></a>'; ?>
<?php if ($menu->children()) : ?> 
        <ul>
        <?php foreach($menu->children() as $child) : ?>
            <li><?php echo $child->link() ?></li>
        <?php endforeach; ?>
        </ul>
<?php endif; ?>
      </li>
<?php endforeach; ?> 
</ul>

Re: Drop Down Menu

Yes David, Thank you very much.

"When you want something, all the universe conspires in helping you to achieve it." - The Alchemist

Re: Drop Down Menu

Sorry, one last question.

Is there a way to make an if statement  so that the articles page does not make a dropdown?

I am kind of a newbie in php

"When you want something, all the universe conspires in helping you to achieve it." - The Alchemist

11

Re: Drop Down Menu

thewizardofbos wrote:

Is there a way to make an if statement  so that the articles page does not make a dropdown?

Yep! Instead of this (line 4, if I've counted right!):

<li<?php if ($menu->children()) { echo ' class="drop"'; } ?>>

change it to:

<li<?php if ($menu->children() && $menu->slug() != 'articles') { echo ' class="drop"'; } ?>>

and then in line 6:

<?php if ($menu->children()) : ?> 

put this:

<?php if ($menu->children() && $menu->slug() != 'articles') : ?> 

= only do the following if $menu has children, and if $menu's "slug" is not "articles".

This kind of test is used in one of the nav cookbook "recipes" which might be worth a look. Also, you could go through the code commentary of the simple navigation (much like the default nav) if you're really new to PHP.

12

Re: Drop Down Menu

Is there a way to sort the children menu order by Page Tilte rather than date published. (Alphabetically)

Bangkok, Thailand

Thumbs up

Re: Drop Down Menu

<?php if ($menu->children(array('order' => 'title ASC'))) : ?>

for example

Wolf CMS founder and lead developer
Please always check the Support forums and Wiki before asking. (My Ohloh account.)
Like Wolf CMS? Consider making a financial contribution.

14

Re: Drop Down Menu

mvdkleijn wrote:

... for example

Which is the example needed, of course. wink For further exploration, there is a write up of this in the wiki.

15

Re: Drop Down Menu

Thank you.

Bangkok, Thailand

Thumbs up

Re: Drop Down Menu

Excellent piece of code!

Thumbs up

Re: Drop Down Menu

hello all

Looking at this code above thinking it's the closest to what i want to achieve.
What i have so far...

<ul id="nav">
      <li<?php echo url_match('/') ? ' class="active"': ''; ?>><a href="<?php echo URL_PUBLIC; ?>">Home<span></span></a></li>

<?php foreach($this->find('/')->children() as $menu): ?>
      <li<?php if ($menu->children() && $menu->slug() != 'seo-articles') { echo in_array($menu->slug, explode('/', $this->url)) ? ' class="active"': null; } ?>>
         <?php echo $menu->link($menu->title.'<span></span>'); ?>
<?php if ($menu->children() && $menu->slug() != 'seo-articles') : ?>

        <ul class="subnav">
        <?php foreach($menu->children() as $child) : ?>
            <li<?php echo in_array($menu->slug, explode('/', $this->url)) ? ' class="active"': null; ?>><?php echo $child->link() ?></li>
        <?php endforeach; ?>
        </ul>

<?php endif; ?>
      </li>
<?php endforeach; ?> 
</ul>

there are 2 elements that I would like to happen...

ok, solved 2 of my bugs... but still have, active being added to all '.subnav' li's

ideally want to have the class 'dualrow' written in the top most ul (#nav) only when there is an active li in the ul.subnav to avoid the mouse-over (the spans display block when containing li has 'active'&'hover' state) overlapping the active ul.subrow.

I haven't got the the bug testing stage but the last time I checked it looked reasonable in later versions of IE.

thanks for anything.


==============

Well I have got it doing what i wanted  mostly...

<ul id="nav">

<li<?php echo url_match('/') ? ' class="active"': ''; ?>><a href="<?php echo URL_PUBLIC; ?>">Home<span></span></a></li>

<?php foreach($this->find('/')->children() as $menu): ?>

<li<?php if ($menu->link() && $menu->slug()) echo in_array($menu->slug, explode('/', $this->url)) ? ' class="active"': null; ?>>
 
<?php echo $menu->link($menu->title.'<span></span>'); ?>
<?php if ($menu->children() && $menu->slug() != 'articles') : ?> 
      
<ul class="subnav">
<?php foreach($menu->children() as $child) : ?>
<li<?php echo in_array($child->slug, explode('/', $this->url)) ? ' class="active"': null; ?>><?php echo $child->link($child->title.'<span></span>') ?></li>
<?php endforeach; ?>
</ul>

<?php endif; ?>
</li>
<?php endforeach; ?> 
</ul>

Last edited by Tony_mk2 (2011-02-15 04:15)

Thumbs up