Controlling navigation menu items display
7 posts by 3 authors in: Forums > CMS Builder
Last Post: January 14, 2017 (RSS)
Hi,
I want to hide some items on my navigation menu. I tried using a where clause for the 'display' checkbox that I added in the Section Editor but CMSB would not allow me. How do I achieve this?
// load records from 'nav_menu'
list($nav_menuRecords, $selectedNav_menu) = getCategories(array(
'tableName' => 'nav_menu', //
'categoryFormat' => 'showall', // showall, onelevel, twolevel, breadcrumb
'defaultCategory' => 'first', // Enter 'first', a category number, or leave blank '' for none
'where' => 'display = 1',
?>
Thanks
Terry
I found a solution, which I've highlighted in red below, but if there is a simpler option please let me know.
I need a solution for one other thing. I need to insert a CSS class in one of the parent tags - "Festive Menu". How can I achieve that?
Thanks
Terry
<nav class="nav">
<ul class="sf-menu" data-type="navbar">
<?php foreach ($nav_menuRecords as $categoryRecord): ?>
<?php if ($categoryRecord['display']==1): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<a href="<?php echo $categoryRecord['link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="<?php echo $categoryRecord['link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</nav>
By Damon - December 13, 2016
Hi Terry,
Give this code a try:
<?php $class = "" //setup class variable ?>
<?php if ($categoryRecord['num']== 5): // if a specific category record matches then assign a class name ?>
<?php $class = "class_name_here"; ?>
<?php endif; ?>
Change the category num from 5 to match the record number for "Festive Menu".
Then print out the class name where you need it. It will only appear if the category matches
<?php echo $class; ?>
Hopefully this works for what you need.
For the other requirement, if you need to hide records, you could add the a Hidden checkbox in your Section Editor so when checked, that record will not be displayed on the website. This lets users temporarily make records visible or not.
http://www.interactivetools.com/docs/cmsbuilder/special_fieldnames.html
Damon Edis - interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Thanks Damon,
They both work excellently! That 'hidden' field is brilliant programming in the CMSB!
Terry
Hi,
A problem has arisen with the child items in the navigation menu using the code below.
<nav class="nav">
<ul class="sf-menu" data-type="navbar">
<?php foreach ($nav_menuRecords as $categoryRecord): ?>
<?php if ($categoryRecord['display']==1): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<a href="<?php echo $categoryRecord['link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="<?php echo $categoryRecord['link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</nav>
The above code produces the following HTML structure:
<nav class="nav">
<ul class="sf-menu" data-type="navbar">
<li><a href="x.php">Home</a></li>
<li><a href="">Festive Menus</a>
<ul>
<li><a href="/download/2016_psb_festive_menu.pdf">Festive menu</a></li>
<li><a href="/download/2016_psb_christmas_lunch.pdf">Christmas lunch</a></li>
</ul>
</li>
<li><a href="#">Menus</a>
<ul>
<li><a href="y.php">Child Menu Item</a></li>
<li><a href="y.php">Child Menu Item</a></li>
<li><a href="y.php">Child Menu Item</a></li>
<li><a href="y.php">Child Menu Item</a></li>
</ul>
</li>
<li><a href="/reservations.php">Reserve a table</a></li>
<li><a href="/contact.php">Contacts</a></li>
</ul>
</nav>
However, I want to hide 'Christmas Menu' in the Festive Menus dropdown list. I do that by unchecking the 'Display' box in the Section Editor (see attached screenshot). When I do that it produces the following HTML structure which alters the original structure because it is removes the closing </ul> & ,/li> tags in this dropdown list and subsumed the items which follow as children and grandchildren (??) in this list. This is a built-in CMSB thing. I need to rectify this. How?
<nav class="nav">
<ul class="sf-menu" data-type="navbar">
<li><a href="/home.php">Home</a></li>
<li><a href="">About</a>
<ul>
<li><a href="/aboutus.php">Our Restaurant</a></li>
<li><a href="/subscribe.php">Newsletter subscription</a></li>
</ul>
</li>
<li><a href="">Festive Menus</a>
<ul>
<li><a href="/download/2016_psb_festive_menu.pdf">Festive menu</a></li>
</ul> } These are stipped out. The effect is that the items below
</li> } become children and grandchildren (??) in this dropdown list.
<li><a href="#">Menus</a>
<ul>
<li><a href="y.php">Child Menu Item</a></li>
<li><a href="y.php">Child Menu Item</a></li>
<li><a href="y.php">Child Menu Item</a></li>
<li><a href="y.php">Child Menu Item</a></li>
</ul>
</li>
<li><a href="/reservations.php">Reserve a table</a></li>
<li><a href="/contact.php">Contacts</a></li>
</ul>
</nav>
Thanks
Terry
Hey Terry,
This is happening because the li and ul tag HTML in the variables _listItemStart and _listItemEnd are created when the getCategories function runs. But the code that's being used to decide if the ul and li tags should be displayed is running after the tags have been created:
<?php if ($categoryRecord['display']==1): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<a href="<?php echo $categoryRecord['link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="<?php echo $categoryRecord['link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endif; ?>
So when records with display set to 1 are not displayed, the ul and li HTML no longer align correctly.
The getCategories function doesn't allow you to pass a where statement into it, so the best solution is to update the section in the CMS so that it use the Hidden checkbox (mentioned in the post by Damon above) instead of the display checkbox, then the records you don't want displaying will be automatically filtered out by the getCategories function, and you won't have to use an if statement to stop the records displaying.
Thanks,
Greg
PHP Programmer - interactivetools.com
Greg,
That's a brilliant piece of programming in CMSB. Makes life so much easier.
This solution works well.
Thanks
Terry