Category Menus - How to highlight active button

13 posts by 4 authors in: Forums > CMS Builder
Last Post: February 28, 2011   (RSS)

By (Deleted User) - February 21, 2011

I have a category menu as the menu structure for my site.
Beta here: http://www.bostonlimos.co

With a regular menu, you can use CSS to have the "active" button on a given page highlighted (for example, on the index.php page, the "Home" button would be blue, on the services page, the "Services" button would be blue, and the "Home" button would be white again.)

What is the proper way to go about this when the menu is in PHP? I'm only looking for one of the five horizontal menu buttons at the top to be selected when a page under that category is being viewed.

Re: [Jason] Category Menus - How to highlight active button

By (Deleted User) - February 22, 2011

Here is the menu that is on the site.
Attachments:

menu.php 2K

Re: [jkoveos] Category Menus - How to highlight active button

By Jason - February 23, 2011

Hi,

What you can do is on each of your .php page, you can set a $_REQUEST variable just before you include menu.php.

For example:
<?php $_REQUEST['currentPage'] = "/index.php"; ?>
<?php include("menu.php");?>


You can then compare this variable to the url of the url field in your category menu like this:

<div class="menuh">
<ul>
<?php foreach ($menuhRecords as $record): ?>
<?php echo $record['_listItemStart'];?>
<a href="<?php echo $record['url'] ?>" <?php if(@$_REQUEST['currentPage'] == $record['url']) { echo 'class = "active"';} ?> ><?php echo $record['name'] ?></a>
<?php echo $record['_listItemEnd'];?>

<?php endforeach ?>

<?php if (!$menuhRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
</ul>

</div>


Hope this gets you started. You may need to change this code depending on how you're using your CSS.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Category Menus - How to highlight active button

By aev - February 24, 2011

Hi,

isn't the $categoryRecords['_isSelected'] meant to be used for handling this?

I use this field in conjunction with the new ulAttributesCallback/liAttributesCallback introduced in CMSB 2.06 to handle category menus.

-aev-

Re: [aev] Category Menus - How to highlight active button

By Jason - February 24, 2011

Hi,

Yes, you can use _isSelected, but to do that, the link you click on has to pass something in the url that specifies which category link was just clicked. In code provided, they're using a text field called url to create the link.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Category Menus - How to highlight active button

By zip222 - February 27, 2011

I have another question about the category records.

I have a menu that allows up to two levels. When a second level page is selected I want the parent to also stay highlighted. example...

Home
About
- Company
- Vision (selected)
- Staff
Products
Services

If i am on the "Vision" page, I want "Vision" and "About" to both be highlighted. How do I do this? Here is the code I am currently using:

// load main navigation
list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'pages',
'categoryFormat'=> 'onelevel',
));


<ul>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>

<?php if ($categoryRecord['_isSelected']): ?>
<span class="current">
<a href="?page=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>
</span>
<?php else: ?>
<a href="?page=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>

Re: [zip222] Category Menus - How to highlight active button

By aev - February 28, 2011

Hi,

maybe you can use the _isSelectedBranch field.

Try changing your line 4 to this:
<?php if ($categoryRecord['_isSelected'] || $categoryRecord['_isSelectedBranch']): ?>


Here are some more fields you can use when checking category relations:

_isSelected
_isAncestorSelected
_isDescendantSelected
_isSelectedBranch
_hasParent
_hasChild
_isFirstChild
_isLastChild
_hasSiblings



-aev-

Re: [aev] Category Menus - How to highlight active button

By zip222 - February 28, 2011

Thanks. The variable I was looking for was _isDescendantSelected. This is super helpful.

Re: [zip222] Category Menus - How to highlight active button

By zip222 - February 28, 2011 - edited: February 28, 2011

Follow up question...

how do I force the first menu item to be selected when first arriving to the page? The correct page is being displayed, but it isn't being highlighted in the menu... unless I click on it, which updates the URL to include the pages "num"

See page:
http://lasasections.jasmardesign.com/template/