Restricting Category Menu to two levels

4 posts by 2 authors in: Forums > CMS Builder
Last Post: August 30, 2010   (RSS)

Hi there, having problems getting my navmenu to restrict to two levels. Currently showing all (3 levels).

Any suggestions?

<?php
// load records
list($pagesRecords, $pagesMetaData) = getCategories(array(
'tableName' => 'pages',
'categoryFormat' => 'twolevel',
));

?>

<ul>

<?php $inactiveChild=0; ?>
<?php $childCount=0; ?>
<?php foreach ($pagesRecords as $record): ?>
<?php if($record['active']==1 && $record['hide']==0): ?>
<?php if($inactiveChild && !$record['_hasParent']): ?></ul></li><?php endif ?>
<?php if($record['_hasParent']): ?><?php $childCount++; ?><?php else:?><?php $childCount=0;?><?php endif ?><?php $inactiveChild=0; ?>
<?php echo $record['_listItemStart']; ?><?php
$queryString = str_replace(" ","-",$record['url'])."-".$record['num'];
$queryString = strtolower($queryString);
?><a href="/<?php echo $queryString;?>/"><?php echo htmlspecialchars($record['name']);?></a><?php echo $record['_listItemEnd'];?>
<?php else: ?>
<?php if($record['_hasParent'] && $childCount==0): ?><ul><?php $childCount++; ?><?php endif ?>
<?php if($record['_hasParent']): ?><?php $inactiveChild=1;?><?php else: ?><?php $inactiveChild =0; ?><?php endif ?>
<?php endif ?>
<?php endforeach ?>

</ul>


Many thanks
Jan

Re: [aquaman] Restricting Category Menu to two levels

Just discovered the third level is only showing when its parent is selected - if that helps.

Thanks
Jan

Re: [aquaman] Restricting Category Menu to two levels

By Chris - August 30, 2010

Hi Jan,

You can skip categories which are deeper than the second level by adding a line of code right after your foreach which checks the 'depth' field. Since depth starts at 0, you'll want to skip anything > 1:

<?php foreach ($pagesRecords as $record): ?>
<?php if ( $record['depth'] > 1 ) { continue; } ?>


I hope this helps! Please let me know if you have any questions.
All the best,
Chris