I know this question has probably been asked before and I’ve looked at a few examples/explanations but haven’t been able to get it working properly yet.
I want to display a list of subcategories nested within the parent’s list item but only when on either the page for the parent category or one of the child categories.
This is what I have so far:
{exp:weblog:categories weblog="services" sort="asc" style="linear" dynamic="off" parent_only="yes" show_empty="yes"}
<li><a href="http://{path=detail/category}">{category_name}</a>
{exp:child_categories parent="{category_id}" child_categories_sort_by="custom" child_categories_sort_direction="asc" show_empty="yes"}
{parent_category_start}
parent_category_url_title
{if "{parent_category_children_count}" > 0}<ul class="subnav">{/if}
{parent_category_end}
{child_category_start}<li><a href="http://{homepage}{segment_1}/detail/category/{child_category_url_title}/">{child_category_name}</a></li>{child_category_end}
{parent_wrapper_bottom_start}
</li>
{if "{parent_category_children_count}" > 0}</ul>{/if}
{parent_wrapper_bottom_end}
{/exp:child_categories}
</li>
{/exp:weblog:categories}
</ul>
I know it’s probably going to involve {parent_category_url_title} or maybe another plugin in combination, but how do I only show the submenu under the conditions I mentioned above?
Hi Tyssen,
I want to display a list of subcategories nested within the parent’s list item but only when on either the page for the parent category or one of the child categories.
What you use to find that you are
1) on the page for the parent category;
2) on the page for the one of the child categories ?
When on the parent category page, I can use a URL segment, e.g. /template_group/detail/category/cat_url_title/ but the way things are currently, the subcategories have the same URL, so can’t use a segment to get the parent.
Or should I restructure the subcat URLs to look like /template_group/detail/parent_cat_url_title/category/child_cat_url_title/ ?
To reserve one URL segment for parent category url_title and another URL segment for child category url_title, perhaps would be easiest solution.
If you use only 2 levels of categories, then you can find if you are on parent’s page or on child’s page by using Category Id and Category Info pugins.
Not having much luck so far. If I do this, it’s printing out the ID of the currently viewed category, rather than doing any conditional filtering:
<ul class="sidenav">
{exp:weblog:categories weblog="services" sort="asc" style="linear" dynamic="off" parent_only="yes" show_empty="yes"}
{if cat_status!="Closed"}<li><a href="http://{path={segment_1}/detail}">{category_name}</a>
{exp:category_id category_group="8" category_url_title="{segment_4}"}
{if category_id=="{category_id}"}
{exp:child_categories parent="{category_id}" child_categories_sort_by="custom" child_categories_sort_direction="asc" show_empty="yes"}
{parent_category_start}
{if "{parent_category_url_title}" == {segment_4}}
<ul class="subnav">
{/if}
{parent_category_end}
{child_category_start}<li><a href="http://{homepage}{segment_1}/detail/{if">{child_category_name}</a></li>{child_category_end}
{parent_wrapper_bottom_start}
</li>
{if "{parent_category_url_title}" == {segment_4}}</ul>{/if}
{parent_wrapper_bottom_end}
{/exp:child_categories}
{/if}
</li>
{/if}
{/exp:weblog:categories}
</ul>
I haven’t gotten as far as working out what to do on subcategory pages.
Hi,
I’m having trouble running this excellent plugin on a Windows enviroment (IIS), I suspect it may have something to do with the Windows friendly URL, namely:
http://www.colorsfruit.com/QA/CMS/index.php?/the_colors_foundation/donors/
The error I am getting is:
Error The following tag has a syntax error: {exp:child_categories} Please correct the syntax in your template.
As it runs fine on a linux / apache server.
Here’s the template code used:
<li>
<a href="http://index.php?/{segment_1}/purpose%22class=%22open_3" class="open_3">Purpose</a>
<ul id="subSubNav3">
{exp:child_categories sibling="11" show_empty="yes" parse=“inward” status="open"}
{entries_start}
{if {child_category_id} == 11 && segment_2 == "purpose"}
<li><a href="http://index.php?/{parent_category_url_title}/{child_category_url_title}/{url_title}">{title}</a></li>
{/if}
{entries_end}
{/exp:child_categories}
</ul>
</li>
I’m also using the pages module.
Any ideas on why is is happening?
Hi Tyssen,
In your template several things are wrong:
1) if you nest exp:child_categories inside exp:category_id, the you should add
parse="inward"
to exp:child_categories tag.
2) exp:category_id tag should have “weblog” parameter
3) {category_id} variable is parsed by both exp:category_id and exp:weblog:categories tag
3) {category_id} variable is parsed by both exp:category_id and exp:weblog:categories tag
Yeah, I was wondering about that. So how do I go about matching the category and/or its parent against a URL segment? The code updated with the things you pointed out now looks like:
<ul class="sidenav">
{exp:weblog:categories weblog="services" sort="asc" style="linear" dynamic="off" parent_only="yes" show_empty="yes"}
{if cat_status!="Closed"}<li><a href="http://{path={segment_1}/detail}">{category_name}</a>
{exp:category_id category_group="8" weblog="services" category_url_title="{segment_4}"}
{if "{category_id}"=="{category_id}"}
{exp:child_categories parent="{category_id}" child_categories_sort_by="custom" child_categories_sort_direction="asc" show_empty="yes" parse="inward"}
{parent_category_start}
{if "{parent_category_url_title}" == {segment_4}}
<ul class="subnav">
{/if}
{parent_category_end}
{child_category_start}<li><a href="http://{homepage}{segment_1}/detail/{if">{child_category_name}</a></li>{child_category_end}
{parent_wrapper_bottom_start}
</li>
{if "{parent_category_url_title}" == {segment_4}}</ul>{/if}
{parent_wrapper_bottom_end}
{/exp:child_categories}
{/if}
</li>
{/if}
{/exp:weblog:categories}
</ul>
If you want to display categories from the weblog “services”, then you can achieve it by using “category_group” parameter of exp:child_categories tag.
I could if I could get my head around how this should work, but the more plugins I need to use, the less I’m getting what I should do.
Have you (or anyone else) got a basic example of how a two category level navigation that only shows the 2nd level when you’re on a page for that category’s parents or siblings should work? It’s for situations like these that I’d normally use the Structure module, but I don’t have that option in this case.
Try this code:
{exp:child_categories category_group="my_cat_group_id" child_categories_sort_by="custom" child_categories_sort_direction="asc" show_empty="yes" parse="inward"}
{parent_category_start}
{parent_category_name}
{parent_category_end}
{child_category_start}
{if "{parent_category_url_title}" == "{segment_4}"}
{child_category_name}
{/if}
{child_category_end}
{/exp:child_categories}
This code will always display all parent categories of certain category group and will display subcategories of certain parent category in case url-title of that parent category is contained in the fourth URL segment.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.