1

Topic: [SOLVED] 2 levels menu...

I would like to design a 2 levels menu...

The html code must be like that:

                <div>
                    <h3>
                        <a href="http://page1.html">
                            Page 1
                        </a>
                    </h3>
                    <ul>
                        <li>
                            <a href="http://sub-page1.html">
                                Sub page 1
                            </a>
                        </li>
                        <li>
                            <a href="http://sub-page2.html">
                                Sub page 2
                            </a>
                        </li>
                    </ul>
                </div>

And here come's my code (doesn't work):

           

<?php $subPageId = explode('/', $_SERVER['REQUEST_URI']); $level2=$subPageId[1]; $level3=$subPageId[2]; $level4=$subPageId[3]; ?>

                <div>
                    <h3><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">Home</a></h3>
                </div>                    
<?php foreach($this->find('/')->children() as $menu): ?>
                <div>
                    <h3><?php echo $menu->link($menu->title, (in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null)); ?></h3>
 
    <?php if ($level2 != '' && strpos($_SERVER['REQUEST_URI'],$menu->slug) == true) : ?>
    <?php $page2 = $this->find($level2); ?>
 
    <ul><!-- child level {2} -->
    <?php foreach ($page2->children(array()) as $menu2): ?>
    <li><?php echo $menu2->link(); ?> 
    </li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
                </div>
            </div>

Is somenone can help me

Last edited by oweb (2010-03-04 22:10)

My Wolf CMS website (not finished)
http://wolfcms.office-web.net/

Thumbs up

Re: [SOLVED] 2 levels menu...

First thing, not sure if you just haven't copied it fully but there is a <?php endforeach; ?> missing from after the last </div>

Also are you wanting to show the subpages for the current page you are on or just list all the pages out that are in the database?

3

Re: [SOLVED] 2 levels menu...

Now the page isn't blank, but I haven't the complete menu (just a part of primary level)...

My Wolf CMS website (not finished)
http://wolfcms.office-web.net/

Thumbs up

Re: [SOLVED] 2 levels menu...

Ok, that's step one..

Is the full menu you want to show or just the sub pages for the current page.
E.g.

Home
-- About
--- About sub page
--Articles
--- Articles sub page

OR

I.E. You are on about page the menu would look like

Home
-- About
--- About sub page
--Articles

Click onto articles and it changes to

Home
-- About
--Articles
--- Articles sub page

Last edited by snsmurf (2010-03-04 15:35)

5

Re: [SOLVED] 2 levels menu...

Maybe it could be

Home
- About
- Articles
-- Articles sub page
- More
-- More sub page

etc.

My Wolf CMS website (not finished)
http://wolfcms.office-web.net/

Thumbs up

6

Re: [SOLVED] 2 levels menu...

Try this in a snippet, and call the snippet in your Layout:

<div><!-- start 2-level nav -->
<h3><a href="<?php echo URL_PUBLIC; ?>">Home</h3>
<?php foreach($this->find('/')->children() as $menu): ?>
   <h3><?php echo $menu->link(); ?></h3>
<?php $smenu = $this->find($menu->slug());
    if ($smenu->childrenCount() > 0) : ?>
<ul>
  <?php foreach ($smenu->children() as $subMenu) : ?>
    <li><?php echo $subMenu->link(); ?></li>
  <?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endforeach; ?> 
</div><!-- end 2-level nav -->

Is that what you want?

Re: [SOLVED] 2 levels menu...

Makes sense now.
Seems like you got it working though. Here's it modified though.

<div><!-- start 2-level nav -->
    <h3><a href="<?php echo URL_PUBLIC; ?>">Home</h3>
    <?php foreach($this->find('/')->children() as $menu): ?>
           <h3><?php echo $menu->link(); ?></h3>
        <?php if($menu->childrenCount() > 0) : ?>
            <ul>
              <?php foreach ($menu->children() as $subMenu) : ?>
                <li><?php echo $subMenu->link(); ?></li>
              <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    <?php endforeach; ?> 
</div><!-- end 2-level nav -->

Because you are using a foreach there is no need to use:
$smenu = $this->find($menu->slug());
$smenu->childrenCount();

So I just replaced it for you to run off $menu, has the same output and is tidier!

8

Re: [SOLVED] 2 levels menu...

Works well guys!
Thank you very much.

My Wolf CMS website (not finished)
http://wolfcms.office-web.net/

Thumbs up