This code was posted on the forum in response to FireFusion’s request for a method to display a category menus with the following structure:
Master Parent Category
# Category
## Sub category
## Sub category
## Sub category
# Category
## Sub category
## Sub category
## Sub category
# Category
## Sub category
## Sub category
## Sub category
In order to output the following (excluding the master parent category):
<ul>
<li class="cat">Category</li>
<li><a href="#">Sub category</a></li>
<li><a href="#">Sub category</a></li>
<li><a href="#">Sub category</a></li>
<li class="cat">Category</li>
<li><a href="#">Sub category</a></li>
<li><a href="#">Sub category</a></li>
<li><a href="#">Sub category</a></li>
<li class="cat">Category</li>
<li><a href="#">Sub category</a></li>
<li><a href="#">Sub category</a></li>
<li><a href="#">Sub category</a></li>
</ul>
Using in-built tags to generate the menu
<txp:category_list parent="master-parent" exclude="master-parent" children="0" wraptag="ul" break="">
<li class="cat"><txp:category title="1" />
<txp:category_list parent='<txp:category />' exclude='<txp:category />' wraptag="ul" break="" class="subcat_list">
<li><txp:category title="1" link="1" /></li>
</txp:category_list>
</li>
</txp:category_list>
The above code should be self-explanatory, but to summarise – the master parent and children categories are first excluded from the list of categories with the wraptag <ul>
applied. Then, the category title is displayed with the <li class="cat">
applied. Finally, a second level <ul>
is created with the current parent category excluded, followed by a list of categories wrapped in <li>
tags.
Create an active state for the current link
If you would like to create an active state for the current category in the menu, replace this code:
<li><txp:category title="1" link="1" /></li>
With this:
<li<txp:if_category name='<txp:category />'> class="active"</txp:if_category>><txp:category title="1" link="1" /></li>
How would you limit the amount of child categories say to 4 and the have a view more link which would then output the remaining categories, so it would be something similar to this business directory,
www.athlone.ie
I can’t get the class active to display as such. i have a css style with this:
and as css:
Hi Bici, saw you forum post about the problem.
Looks to me like you have a nested
ul
inside the<ul id="subnav_products">
, so you may need to adjust the rule to:Note the extra
ul
. See if that works. If not, can you post the HTML output?still not working. I got rid of the second need for the UL. Now I only have:
and CSS:
What is the HTML output from page source?
PS when i code the category listing like this:
it works fine.
Bici, the second example works because your rule is for the anchor (a) tag:
If you change it to:
Then it should work for the first example, which sets the active class to the
li
tag:<li class="active">
. Hope this helps!Yes! Many thanks. I had some other leftnava css that was interfering with the out put. it now works.
P.S. I am starting to “get” TxP a little more after this exercise
Hi,
Is it possible to have an active class for the top category? Like this:
Hi –
i added the following to my form that handles navigation:
Does the code require anything else? I copied and pasted the code and it does not work.
Thanks, it was a great tip, helped me a lot.