List Child Categories of a Parent Category
Posted: 13 May 2007 11:03 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  55
Joined  02-21-2006

For what it’s worth, thought I’d post this code in case it helps another newbie. I just wanted to list the child categories (ie, subcategories )of a parent category in a bulleted list. Needed it to remain dynamic since the client will be adding child categories over time. It uses the query module.

The following example will retrieve the child categories of the parent category with an id of 42. Note that the “C” in the linked URL MUST be uppercase (thanks Robin!). This example uses a template named “team” to display the category pages, and I have the my_template_group variable defined at the top of my template file like {assign_variable:my_template_group=“meetingarchive”}. Edit the URL and parent ID to suit your needs.


<ul>
{exp:query sql="SELECT cat_id as category_id, cat_name AS category_name FROM exp_categories WHERE parent_id = '42'"}
<li><a href="{path={my_template_group}/team/C{category_id}}">{category_name}</a></li>
{/exp:query}
</ul>

 Signature 

Loracs Creations Inc.
Web Design & Development
http://loracs.com

Profile
 
 
Posted: 21 May 2007 08:39 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Rank
Total Posts:  40
Joined  04-06-2007

Thanks for this.

I just made a wiki page with your code, and giving you credit.

Profile
 
 
Posted: 21 May 2007 08:49 AM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1590
Joined  01-05-2007

I think the alternative is to create a few Category Groups and assign them to a particular weblog.

The Category Group name is like the Parent name then.

 Signature 

 
Steven Hambleton | ExpressionEngine Development for Web & Graphic Design Agencies

Profile
 
 
Posted: 21 May 2007 10:57 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  55
Joined  02-21-2006
stinhambo - 21 May 2007 08:49 AM

I think the alternative is to create a few Category Groups and assign them to a particular weblog.

The Category Group name is like the Parent name then.

Yes, that’s what I ultimately ended up doing, and it worked really well. (See this thread.) It’s nice to have options, depending on what suits your brain and particular purpose!

 Signature 

Loracs Creations Inc.
Web Design & Development
http://loracs.com

Profile
 
 
Posted: 04 February 2008 10:39 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1562
Joined  08-02-2006
stinhambo - 21 May 2007 08:49 AM

I think the alternative is to create a few Category Groups and assign them to a particular weblog.

The Category Group name is like the Parent name then.

Can you maybe go into further detail on this? You can view my question here: http://expressionengine.com/forums/viewthread/70743/ and what I’m trying to do.

Thanks,
Deron

 Signature 

Deron Sizemore
==========
Random Jabber | Sizcons | NiceStylesheet | Kentucky Golfing | LogoGala
Twitter: Deron Sizemore | LogoGala

Profile
 
 
Posted: 05 February 2008 12:45 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005

Deron, did you see this post? It is the one which suggests uses multiple category_groups (acting as/instead of parents) all assigned to a common weblog, then filtering by category_group id in the templates to single out the “children”. (that is, it circumvents the parent/child category issue entirely.) That’s how PDM finally wound up doing it - not by using his own sql query mentioned in the wiki entry.

At least, that’s how I’m understanding it.

Terry

Profile
 
 
Posted: 05 February 2008 07:34 AM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1562
Joined  08-02-2006

Terry,

Ah, yes I actually did read it last night, but I guess it was to late and I had be staring at the computer for too long because it flew right over my head. Makes sense now, but now that it does make sense, I think I like your suggested method in the other thread better. I’m thinking that I just don’t want to create another 5-6 category groups. Seems like extra clutter to me…

Thanks

 Signature 

Deron Sizemore
==========
Random Jabber | Sizcons | NiceStylesheet | Kentucky Golfing | LogoGala
Twitter: Deron Sizemore | LogoGala

Profile
 
 
Posted: 05 February 2008 09:14 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005

To me, too. And a category parent/child setup makes for something you can add to on the fly from the publish page (the edit categories link is there at the bottom of categories, making it available to clients as well, without having to delve into the Admin CP).

But both methods probably have merits. I’m keeping both in my head for future reference!

Terry

Profile
 
 
Posted: 10 July 2008 08:35 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  112
Joined  04-21-2008

{path={my_template_group}/team/C{category_id}}

This works great, however I want to get one more thing out of this:

Once on my template (after clicking on the specific subcategory) - I want to have (that specific)Category Name as a header.

I’m trying:

{exp:weblog:categories weblog="directory"}
{category_name}
{
/exp:weblog:categories}


However, that lists all my existing categories (as expected).

Do I have to work on a SQL query for this, or is there an easier path?

 Signature 

Good Design is What Good Business is all About!

Profile
 
 
Posted: 10 July 2008 09:26 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005

See the exp:weblog:entries categories variable pair. It has a category_name output, as well as category_id, and if you use limit=“1”, it will show them for only the current entry on a single-entry page, or for each entry in a multi-entry page if limit is higher than 1.

I hope that helps!

Terry

Profile
 
 
Posted: 21 July 2008 11:27 AM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  112
Joined  04-21-2008

Thanks, Terry for your help. That works well, however I have to get my categories sorted-out as they’re sort of way deep.

Cheers!

 Signature 

Good Design is What Good Business is all About!

Profile
 
 
Posted: 01 August 2008 03:23 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  112
Joined  04-21-2008

Here’s a much easier solution. I was trying way to hard.

First, I created a new Category Group. So I had no need to do SQL drilling.

I my landing news page; for titles I used:

<a href="{path={my_template_group}/news_page/{categories}C{category_id}{/categories}/{url_title}}">{title}</a>

...which, helps assigning the right breadcrumb once on the news_page. Which (the breadcrumb), looks like this:

{exp:weblog:category_heading}<a href="{path={my_template_group}/news_categories}">{category_name}</a>{/exp:weblog:category_heading}

...and this will only work, because up on the URL I have C(x) - where x is the category_id...

I mean, I LOVE EE for making my life easy!

 Signature 

Good Design is What Good Business is all About!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64939 Total Logged-in Users: 60
Total Topics: 81912 Total Anonymous Users: 40
Total Replies: 440330 Total Guests: 275
Total Posts: 522242    
Members ( View Memberlist )