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.

Show Child Categories if Not Empty

June 28, 2013 1:19pm

Subscribe [1]
  • #1 / Jun 28, 2013 1:19pm

    AirWatch

    26 posts

    I have a sidebar template that pulls in a bunch of parent categories in an accordion menu style. When you click on a parent, it expands to show the child categories.

    The template code for that is (simplified version took out HTML for accordion)

    {exp:channel:categories channel="customers" style="linear" parent_only="yes" show_empty="no"}
      <!-- Accordion Title Menu -->{category_name}
    
      <!-- Embed Template to show the child categories in expanded view -->
      {embed="customers/sidebar-sub-nav" parent_cat_url="{category_url_title}" parent_cat_id="{category_id}"}
    
    {/exp:channel:categories}

    So you can see that to display the child categories there is an embedded template. This template uses a query to get the child categories. In this template I need to display only the child categories that are not empty (show_empty=no). How can I achieve this?

    <ul>
        {exp:query sql="SELECT
        cat_id AS child_category_id,
        cat_name AS child_category_name,
        cat_url_title AS child_category_url_title
        FROM exp_categories
        WHERE parent_id = {embed:parent_cat_id}"}
        <li><a href="/{segment_1}/{embed:parent_cat_url}/{child_category_url_title}">{child_category_name}</a></li>
        {/exp:query}
    </ul>

    Right now, this shows me all the child categories - even the ones that are empty.


    If needed, the full sidebar code with the HTML for the accordion:

    <aside class="grid columns-3 sidebar">
      <nav class="sidebar-nav">
            <div id="customers-accordion" class="accordion">
                {exp:channel:categories channel="customers" style="linear" parent_only="yes" show_empty="no"}
                    <div class="accordion-group">
                        <div class="accordion-heading">
                            <a href="#collapse-{count}" class="accordion-toggle {if category_url_title == ">_                          {category_name}_                        </a>
                        </div><!-- /accordion-heading -->
                        <div class="accordion-body collapse {if category_url_title == "{segment_2}"}in{/if}" id="collapse-{count}">
                            <div class="accordion-inner">
                                {embed="customers/sidebar-sub-nav" parent_cat_url="{category_url_title}" parent_cat_id="{category_id}"}
                            </div><!-- /accordion-inner -->
                        </div><!-- /accordion-body -->
                    </div><!-- /accordion-group -->
                {/exp:channel:categories}
            </div><!-- /customers-accordion -->
      </nav>
    </aside>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases