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 Child Categories Problem

January 31, 2012 2:05pm

Subscribe [4]
  • #1 / Jan 31, 2012 2:05pm

    Deep Arora

    153 posts

    I wish to display the child categories only of the selected parent category in my categories bar on the side. The main category (selected) is to be read from the URL (e.g. domain.com/docs/category/admin). So when this URL is called, all the child categories of parent category “admin” need to be pulled and shown.

    How can I achieve this?

  • #2 / Jan 31, 2012 2:44pm

    RevaCo

    240 posts

    Perhaps with some conditionals based on the value of your url segment you may be able to use the show= parameter.

    Docs: Categories > Show

    Was thinking along the lines of using show=“not 2” for example.

    The marvellous Switchee from Mr Croxton may help with the logic.

  • #3 / Feb 01, 2012 3:39pm

    Dan Decker

    7338 posts

    Hi Deep Arora,

    What you want is not possible in ExpressionEngine out-of-the-box, however GWcode Categories can do precisely what you are looking for.

    Let me know what you think!

    Cheers!

  • #4 / Feb 01, 2012 3:50pm

    Deep Arora

    153 posts

    Yes, I found that last night and it works perfect. Thanks for mentioning it though.

    I’m stuck at another problem with it now. When I select the parent category, it display sthe child cats in sidebar correctly. But when I open a post/entry, all cats disappear. Because now I need to find the main parent of the post/entry so I can pass it to side bar to pull correct child cats. Is it possible to get the mai parent cat id in the entry loop in template?

  • #5 / Feb 04, 2012 10:17am

    Deep Arora

    153 posts

    Anyone ?

  • #6 / Feb 05, 2012 7:19am

    Sean C. Smith

    3818 posts

    Hi Deep Arora,

    I would recommend talking to the add-on developer for assistance with their add-ons code as I am unfortunately unfamiliar with that addon.

    Sean

  • #7 / Feb 06, 2012 1:55pm

    Leon Dijk

    131 posts

    Hi Deep,

    When you say main parent, you mean the root category in which an entry has been posted?

    So lets say you have:

    Cat A
    —Cat A1
    Cat B
    —Cat B1
    ——Cat B1.1
    ——Cat B1.2

    and you added the entry to Cat B1.2, you want to get the Cat B category and then show child categories for that one?

    If so, this might do the trick:

    {exp:channel:entries channel="test" dynamic="no" entry_id="2" limit="1"}
     {!-- Make sure you get the correct entry, by changing the above parameters to what you need --}
     {title}
    
     Categories:
     {categories limit="1"}
      {exp:gwcode_categories cat_id="{category_id}" parse="inward"}
       {cat_name}
    
      {/exp:gwcode_categories}
     {/categories}
    {/exp:channel:entries}
  • #8 / Feb 08, 2012 7:42am

    Deep Arora

    153 posts

    Let me show you exactly what I am doing:

    1. On this page:
    http://www.invanto.com/index.php/docs/category/sitemanpro

    The sub categories (on the left) of the selected product (from the top menu should be displayed). If I select a different product (WPSuite or EESuite) from the top menu, their sub cats should be displayed.

    This part is working fine using this code for the categories on the left:

    <ul>
    {exp:gwcode_categories cat_url_title="{segment_3}" incl_self="no" style="linear" cat_id="{embed:cat_id}"}
    
    <li><a href="http://{path=docs/category}/{cat_name}class=active">{cat_name}</a></li>
    
    {/exp:gwcode_categories}
    </ul>

    2. When you open a entry to read, the cats on the left should be displayed of the parent category of the opened document. For example, if I open the document (http://www.invanto.com/index.php/docs/guide/getresponse-integration) then sub cats should be displayed of the parent cat of the select document. THIS IS ALSO WORKING.

    But what is not what working in this point is that the parent category at the top is lost (not highlighted with a little top arrow pointer). But I can live with it if not doable.

    The code I am using to display categories on this page is (within exp:entry loop)

    <ul>
    {categories limit="1"}
      {exp:gwcode_categories cat_id="{category_id}" parse="inward" style="linear" incl_self="no" }
       <li><a href="/docs/category/{cat_name}class=active">{cat_name}</a></li>
      {/exp:gwcode_categories}
     {/categories}
    </ul>

    3. THIS IS THE MAIN PROBLEM - Now when I click on any of the sub cats in the left side menu, though it displays the documents of the clicked category, all the sub cats vanish when page reloads because now it tries to load the children of the clicked category. What I want is keep the menu same if a child category is clicked.

    For example, if you click the ADMINSTRATION sub cat on the left, and visit this page (http://www.invanto.com/index.php/docs/category/Administration) none of the left side cats are shown. Moreover, the top horizontal parent category reference is also lost.

    Is there a way to resolve this?

  • #9 / Feb 08, 2012 8:50am

    Leon Dijk

    131 posts

    Hi Deep,

    Let’s first try to tackle problem 3.

    The problem is that when you call http://www.invanto.com/index.php/docs/category/Administration, it will use {segment_3} (Administration) as the parameter value for the gwcode_categories tag, meaning that it will try to show child categories for Administration, which don’t exist as Administration is already a (last) child category, so it doesn’t output anything.

    So, what you need to do is figure out if the category you’re viewing is a root category, or a child category and then use different code for both situations.

    I would do this:

    Install LowSeg2Cat

    Then, something like this for the code for the categories on the left:

    <ul>
    {if segment_3_category_parent_id == 0}
     {!-- we are on a root category page --}
     {exp:gwcode_categories cat_url_title="{segment_3}" incl_self="no" style="linear"}
      <li><a href="http://{path=docs/category}/{cat_name}class=active">{cat_name}</a></li>
     {/exp:gwcode_categories}
    {if:else}
     {!-- we are on a child category page --}
     {exp:gwcode_categories cat_id="{segment_3_category_parent_id}" incl_self="no" style="linear"}
      <li><a href="http://{path=docs/category}/{cat_name}class=active">{cat_name}</a></li>
     {/exp:gwcode_categories}
    {/if}
    </ul>

    Could you give that a try to see what that does?

    Edit - Made a small error in the code I provided, just updated it.

  • #10 / Feb 08, 2012 9:33am

    Deep Arora

    153 posts

    Awesome…worked with a little change:

    <ul>
    {if segment_3_category_parent_id == 0}
     {!-- we are on a root category page --}
     {exp:gwcode_categories cat_url_title="{segment_3}" incl_self="no" style="linear"}
      <li><a href="http://{path=docs/category}/{cat_name}class=active">{cat_name}</a></li>
     {/exp:gwcode_categories}
    {if:else}
     {!-- we are on a child category page --}
     {exp:gwcode_categories cat_id="{segment_3_category_parent_id}" incl_self="no" style="linear"}
      <li><a href="http://{path=docs/category}/{cat_name}class=active">{cat_name}</a></li>
     {/exp:gwcode_categories}
    {/if}
    </ul>

    Thanks. Any suggestions for the other issues?

  • #11 / Feb 08, 2012 10:35am

    Leon Dijk

    131 posts

    Great! Seems like you’ve changed it exactly to what I suggested (I edited my post as I discovered the error in it).

    Ok, now for pages such as this one: http://www.invanto.com/index.php/docs/category/Administration

    you need to do something like this:

    {exp:channel:categories channel="test" parent_only="yes"}
     <a href="#" class="active">{category_name}</a>
    {/exp:channel:categories}

    Not sure why, but I needed to put segment_2_category_id in between quotes to get it to work.

    Could you give that a try to see what that does?

  • #12 / Feb 08, 2012 10:36am

    Leon Dijk

    131 posts

    Grr, forum doesn’t let me post proper if statements again :( Please hold while I generate a PDF for it.

  • #13 / Feb 08, 2012 10:37am

    Leon Dijk

    131 posts

    Here you go, see attachment.

  • #14 / Feb 08, 2012 11:50am

    Deep Arora

    153 posts

    Works perfect 😊
    http://www.invanto.com/index.php/docs/category/Administration

    Code:

    {if segment_3_category_parent_id == 0}
     {!-- we are on a root category page --}
     {exp:gwcode_categories cat_url_title="{segment_3}" incl_self="no" style="linear"}
      <li><a href="http://{path=docs/category}/{cat_name}">{cat_name}</a></li>
     {/exp:gwcode_categories}
    {if:else}
     {!-- we are on a child category page --}
     {exp:gwcode_categories cat_id="{segment_3_category_parent_id}" incl_self="no" style="linear"}
      <li>
     <a href="http://{path=docs/category}/{cat_name}" class="active">{cat_name}</a>
      </li>
     {/exp:gwcode_categories}
    {/if}
    </ul>

    Can’t wait for the next step!!

  • #15 / Feb 08, 2012 2:42pm

    Leon Dijk

    131 posts

    Hi Deep,

    Bit confused here, did you try the code in the PDF? It was for the 3 categories at the top of the pages. The code you posted is for the left menu?

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

ExpressionEngine News!

#eecms, #events, #releases