ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Displaying category and its subs/entries

October 25, 2010 5:20pm

Subscribe [2]
  • #1 / Oct 25, 2010 5:20pm

    Rudy Affandi

    9 posts

    Hi all,

    Here’s what I am facing with: I have a category group that has categories and sub categories up to 5 level deep. Example below:

    Canada
    - British Columbia
       - Lower Mainland
          - Vancouver
             - Kitsilano
             - West End
          - Burnaby
             - Capitol Hill
             - North Burnaby
       - Vancouver Island
          - Victoria
          - Nanaimo

    What I want to accomplish is to create a template that allows me to display a category with its subs and entries.

    I also want to reuse this template for any level of categories if it’s all possible.

    For example, when I click “Lower Mainland”, I want to be able to display:

    Lower Mainland
       - Vancouver
          - Kitsilano
          - West End
       - Burnaby
          - Capitol Hill
          - North Burnaby

    Moved to CodeShare Corner by Moderator

    Thanks, any help would be appreciated.
    Rudy

  • #2 / Oct 25, 2010 6:12pm

    Tony Geer

    253 posts

  • #3 / Oct 25, 2010 6:16pm

    Rudy Affandi

    9 posts

    Hi Tony, thanks for your suggestion. I have already tried using this plugin and it works but only able to display category and its immediate children. I need to be able to display the children and its children. Basically similar to the output of category_archive except that it should only display the selected category down, and not showing the parent.

    Cheers
    Rudy

  • #4 / Oct 25, 2010 6:21pm

    Tony Geer

    253 posts

    That’s odd, the EE1 version, from which this one was ported, allowed you to view cats, subcats and children.

  • #5 / Oct 25, 2010 6:27pm

    Rudy Affandi

    9 posts

    Hi Tony,

    I was able to display its sub cats, but not sub-sub cats. So basically only able to display 2 level deep. I need to see unlimited levels displayed.

    Thanks
    Rudy

  • #6 / Oct 25, 2010 6:28pm

    Tony Geer

    253 posts

    Oh sorry about that, my fault for not reading your post properly. Good luck with it 😊

  • #7 / Oct 26, 2010 4:38pm

    Ingmar

    29245 posts

    What EE tags are you using? Can you show us a little more of your code?

  • #8 / Oct 26, 2010 5:38pm

    Rudy Affandi

    9 posts

    Thanks for replying Ingmar,

    I have tried many methods. Each method have pros and cons. Here are my many methods so far:

    Method 1: Using category_archive

    {exp:channel:category_archive channel="{master_channel_name}" style="nested" class="cat_list"}
       {categories}
          <strong><a href="http://{path={current_location}/template/category/{category_url_title}}">{category_name}</a></strong>
          {if category_description}{category_description}{/if}
       {/categories}
    
       {entry_titles}
          <a href="http://{path=template/topic/}">{title}</a>
    
       {/entry_titles}
    {/exp:channel:category_archive}

    Using this method, I was able to display all levels of categories which is great, but I would like to be able to click on any sub category and drill down to the next level of sub categories. Unfortunately, category_archive did not let me display just part of a sub category without displaying its parent. It also displays other unrelated categories unless I specified using the “show” parameter.

    Method 2: Using child_category plugin (3rd party)

    {exp:channel:categories category_group="{cat_group}" channel="{master_channel_name}" parent_only="yes" style="linear"}
       <h3><a href="http://{path={current_location}/template/category/{category_url_title}}">{category_name}</a></h3>
    <p>   {if category_description}{category_description}{/if}</p>
    
    <p>    <ul class="topic_list clear"><br />
        {exp:child_categories parent="{category_id}" child_categories_sort_by="custom" child_categories_sort_direction="asc" show_empty="yes" entries_sort_by="date" entries_sort_direction="asc"}<br />
             {child_category_start}<br />
                <li><a href="http://{current_location}/overview/{child_category_url_title}">{child_category_name}</a></li><br />
            {child_category_end}<br />
        {/exp:child_categories}<br />
        </ul><br />
    {/exp:channel:categories}

    Using this method, I was able to drill down each category level but it only displays the current category and its immediate sub-categories. It does not allow me to show all subs and sub-subs.

    Method 3: Using recursive_subcategories plugin (3rd party, I had to make some adjustment to the php code to fix the nested UL tags)

    {exp:recursive_subcategories parent_id="{segment_4_category_id}" show_parent="0" style="linear"}
       <a href="http://{path={current_location}/template/category/{category_url_title}}">{category_name}</a>
       {if category_description}{category_description}{/if},
    {/exp:recursive_subcategories}

    This is a pretty neat little plugin. I was able to drill down categories hierarchy but similar to child_categories, it only displays immediate children of any given category.

    The ideal solution for me would be something like category_archive but with a lot more freedom in the parameters, such as allowing a given category to display its children/grand-children etc without showing its parent.

    I am testing another method using “channel” as a pseudo category placeholder and combine it with “Relationship” to create the parent/children relationship. This will be a lot more work for content population since the content manager has to define the relationship every single time but at this point, I am happy just to get it to work properly.

    Thanks for your help
    Rudy

  • #9 / Oct 27, 2010 3:28pm

    Ingmar

    29245 posts

    Have you tried {exp:channel:categories} with a nested {exp:channel:entries} tag?

    {exp:channel:categories}
       {exp:channel:entries category="{category_id}" ...}
    ...
    {/exp:channel:categories}

    Something like that, anyway. That said a custom query or plugin might in fact be the best way to handle this.

  • #10 / Oct 27, 2010 3:44pm

    Rudy Affandi

    9 posts

    Hi Ingmar,

    I have managed to solved the riddle by using combination of recursive_subcategories plugin (converted from EE1.x) and embed function. I also did some tweaking to the original plugin to allow customization in UL styling for the first item on the list.

    Here’s the main code:

    {exp:recursive_subcategories parent_id="{segment_4_category_id}" show_parent="1" style="nested"}
       <a href="http://{path={current_location}/overview/category/{category_url_title}}">{category_name}</a>
       {embed=includes/related_answer cat_id="{category_id}"}
    {/exp:recursive_subcategories}

    and here is the embed code:

    <ul>
    {exp:channel:entries channel="answers" category="{embed:cat_id}"}
    <li>{title}</li>
    {/exp:channel:entries}
    </ul>

    What this does is essentially list any given category and its sub category hierarchy in multiple level (depends on the number of level you have on your category group) and its entries if any. Thank you for your input btw.

    Ideally, category_archive should be able to do this with additional parameters that allow given parent id, category siblings and current category id.

    Cheers
    Rudy

  • #11 / Oct 27, 2010 4:03pm

    Ingmar

    29245 posts

    Thank you for posting your solution. Feel free to make a Feature Request, too… please post again as needed.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases