I want to use a great website template that uses a normal ul menu but splits in 2 parts and positions the logo in the middle of the menu via css. It does so by using class=“break” on the menu item after the logo. In the example there are 4 menu items and the third one has the class=“break”. Is something like this possible without having to hand-build the menu by myself?
Yes, you could use a counter that if it reaches a certain number, you add the class. If you want to automate this, count the number of element in the collection ($noOfPage=$pages->count()
) and if the counter reaches $noOfPages/2 +1
, add the class.
How do you build the menu at the moment?
I guess it would be possible with a foreach
loop and an incrementing counter to check when to add the break.
<?php var $i = 0; ?>
<nav>
<ul>
<?php foreach($pages as $item): ?>
<?php $i++ ?>
<li <?php e($i == 3, 'class="break"') ?>><a<?php e($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo $item->title()->html() ?></a></li>
<?php endforeach ?>
</ul>
</nav>
I am currently using this for the menu:
<?php foreach($pages->visible() as $p): ?>
<li>
<a <?php e($p->isOpen(), ' class="active"') ?> href="<?php echo $p->url() ?>"><?php echo $p->title()->html($
<?php if($p->hasVisibleChildren()): ?>
<ul class="submenu">
<?php foreach($p->children()->visible() as $p): ?>
<li>
<a href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
</li>
<?php endforeach ?>
Something like this should work:
<?php
$noOfPages = ceil($pages->visible()->count()/2);
$count=0;
?>
<?php foreach($pages->visible() as $p): ?>
<li <?php e($count == $noOfPages, ' class="break"') ?>>
<a <?php e($p->isOpen(), ' class="active"') ?> href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>
<?php if($p->hasVisibleChildren()): ?>
<ul class="submenu">
<?php foreach($p->children()->visible() as $p): ?>
<li>
<a href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
</li>
<?php $count++; endforeach ?>
Edit: I edited the code above to make it work even if the number of pages is odd.
Thanks, but i think you are missing something in this line:
<a <?php e($p->isOpen(), ' class="active"') ?> href="<?php echo $p->url() ?>"><?php echo $p->title()->html($
which results in this error message:
PHP Parse error: syntax error, unexpected '<', expecting variable (T_VARIABLE) or '$' in site/snippets/menu.php on line 11" while reading response header from upstream,
Yes, I just copied the line form your code, but it is faulty:
So this:
<a <?php e($p->isOpen(), ' class="active"') ?> href="<?php echo $p->url() ?>"><?php echo $p->title()->html($
should be corrected to:
<a <?php e($p->isOpen(), ' class="active"') ?> href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>