Jump to content

add 'selected' class to current cms page/category


Recommended Posts

I would like to add class 'selected' to 'a' and I've noticed this principle for Categories block. I would like to do the same for CMS block, but not sure how.


Under category-tree-branch.tpl we have this code that produces 'selected' class when necessary:


{if $node.children|@count > 0}

	{foreach from=$node.children item=child name=categoryTreeBranch}
		{if isset($smarty.foreach.categoryTreeBranch) && $smarty.foreach.categoryTreeBranch.last}
			{include file="$branche_tpl_path" node=$child last='true'}
			{include file="$branche_tpl_path" node=$child last='false'}

[/list]	{/if}


How would I do something similar in blockcms.tpl? Any ideas? what is the correct variable for current cms id and what does $node variable means? Could anybody explain if the code above could be adapted to be used for cms block (if statement for selected class)?

  • Like 1
Link to comment
Share on other sites

I've been trying to find solution online, but no luck just yet.

I have a CMS category with CMS pages underneath and once we are on particular page I want it to have class 'selected'. Anybody please?

How would I use the following code that is used for categories on CMS pages?

<a href="{$node.link}" {if isset($currentCategoryId) && ($node.id == $currentCategoryId)}class="selected"{/if} title="{$node.desc|strip_tags:false|escape:html:'UTF

What is this $node variable? Can anybody explain please?

  • Like 1
Link to comment
Share on other sites

Hello Sixtmind,


Funny : This afternoon I was looking for... the same thing...


I didn't found the totaly response but here is some news :

{if isset($currentCategoryId) && ($cms_page.id_cms == $currentCategoryId)}class="selected"{/if}


You have to replace "$node.id" by "$cms_page.id_cms".

I have tested it, it returns the good cms-page-id.


BUT, what I am looking for now, is the good "current-cms-page-id" to replace "$currentCategoryId" wich doesn't returns the good id in this if-test (in fact, no id is returned by this "$currentCategoryId" in CMS pages).


If I find it before YOU find it, I'll tell you.

And if You find it before I find it, don't forget to tell me...

(sorry about my approximativ English...)

Link to comment
Share on other sites

Hello again,


I found the solution => "$smarty.get.id_cms" !!!


The good code is :

<li><a href="{$cms_page.link}" {if isset($smarty.get.id_cms) && ($cms_page.id_cms == $smarty.get.id_cms)}class="selected"{/if} title="{$cms_page.meta_title|escape:html:'UTF-8'}">{$cms_page.meta_title|escape:html:'UTF-8'}</a></li>


You just have to add the following code in your "global.css" 's Theme :

div.block a.selected { color:#e36731; font-weight:bold; }


Tell me if it's ok for you to.



  • Like 3
Link to comment
Share on other sites

Thanks Remy for sharing your solution and I am glad you made yours work.

I tried your code on my website, but unfortunately it doesn't do exactly what I would like.

Basically I have a 3 CMS blocks in the left column. Each block has pages within in and when I put the code that you used, what it does: it gives all the top pages (CMS categories) class 'selected'. I am still trying to figure how how to give class 'selected' only to the page we are currently on - basically when we are viewing CMS category page.


Edit: Actually it doesn't work at all now, the class selected was added by another code. Your code doesn't do anything on mine. I am looking what the problem is now.

Link to comment
Share on other sites

When it comes to highlighting a menu item, I understand that we need to get a URL of a current page like here:


There is also a smarty variable:

{* display value of page from URL ($_GET) http://www.example.c...ex.php?page=foo *}


but this doesn't seem to work in .tpl files?


Once we have a current page URL, we then check if href value is the same as current page URL. If it is, we add class selected.

Now, this is theory, but how to do it in practice? I've tried:

{if $smarty.get.page == $cms_page.link}class="selected"{/if}

This doesn't work.

I am using this code in blockcms.tpl


Another variable is: $smarty.server.REQUEST_URI which I am not sure how to use.

Link to comment
Share on other sites

  • 4 weeks later...

Hi sixthmind,


Did you have any luck with this?


Basically what i am curious about is... can i add certain CMS pages to blocktopcategories.tpl but while also maintaining the "selected" class when a CMS page is active


Please let me know

Link to comment
Share on other sites

Hi Emmanuel,


Thanks for your reply.

Although it does work, but i have a combination of CMS pages and the categories, meaning that if i have 1 category page selected and try to open a CMS page, the CMS page link does get selected, but the other category page still remains selected.


Is there a way to de-select all the others and have only 1 'selected'?

Link to comment
Share on other sites

  • 2 years later...

Not the perfect solution because you will have to hardcode each link but you could try :


{if $request_uri|strstr:"8"}class="selected"{/if}
where 8 is the ID of your CMS page...


I just tried you solution, it was working well but the number for the cms page was the same with the number of a category page. And because of that when i selected the category, the cms link was on. So i edited your solution and it worked.


I added to your solution:

{$request_uri | strstr:"8-cmspagename"}

So you must add the cms page friendly url not just the ID.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...