Forum

Thread tagged as: Question

Highlight current category in main menu

Using a standard version of Perch. I have a top menu that shows product categories. These all then related to a physical page. When I am on these pages, how can I highlight in the menu the current page. As these are categories and not pages. <perch:if exists="current_page"> won't work.

Here is some code that might help?

<li>
<a href="/<perch:category id="catPath" />" <perch:if id="catPath" match="eq" value="{perch_page_path}">class="current"</perch:if>>
    <perch:category id="catTitle" type="smarttext" label="Title" required="true" />
</a>
</li>

<perch:category id="catSlug" type="slug" for="catTitle" suppress="true" />

<perch:showall />

Full pages live in /products/power/ for example or /products/lighting-display/

here is what the showall is giving;

ID  Value
domain  https://dengrove.loc
url https://dengrove.loc/products/lighting-display/
sitename    Dengrove Marketing Limited
description 
sharing_image   /assets/img/social-sharing-default.jpg
twittername 
og_author   
page_title  Lighting display
perch_page_path /products/lighting-display.php
current_category    false
catPath products/lighting-display/
catID   3
setID   1
catParentID 0
catTitle    Lighting & Display
catSlug lighting-display
catDisplayPath  Lighting & Display
catOrder    3
catTreePosition 001-003
catDynamicFields    {"catPath":"products\/lighting-display\/"}
perch_catPath   products/lighting-display/
catDepth    1
perch_item_zero_index   2
perch_item_index    3
perch_item_rev_index    4
perch_item_rev_zero_index   3
perch_item_odd  
perch_item_count    6
perch_namespace perch:category
Terry Upton

Terry Upton 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You should be able to pass it into the template - presuming you know from the URL what the correct category should be.

PerchSystem::set_var('current_cat', perch_get('whatever_your_category_is_on_the_url'));

and then

<perch:if id="catPath" match="eq" value="{current_cat}"> current </perch:if>

The problem I can't seem to get around is how to get the

perch_get('whatever_your_category_is_on_the_url'));

When I try this;

$url =  perch_page_url(array(
    'hide-extensions'       => true,
    'hide-default-doc'      => true,
    'add-trailing-slash'    => true,
    'include-domain'        => false,
), true);

    PerchSystem::set_var('current_cat', $url);
    echo PerchSystem::get_var('current_cat');

I get the following;

current_cat /products/lighting-display/

but the actual catpath is;
catPath products/lighting-display/

(missing the initial /) and so doesn't match.
Drew McLellan

Drew McLellan 2638 points
Perch Support

So use '/'.$url