1

I have a simple two-level category structure like this:

  • Fresh
    • Fresh Pre Packed
    • Fresh Bulk
  • Frozen
    • Frozen Pre Packed
  • Ready To Go
    • Meals
    • Condiments


I'm simply trying to iterate though the parent categories and for each parent loop through its children and then output the channel entries for that specific category.

I may have overthought this a bit, but right now what I have is this:

    {exp:channel:categories category_group="2" style="linear"}

    <?php
    if({parent_id} == 0) :
        $categories['{category_id}'] = array();
    else :
        if(isset($categories['{parent_id}'])) $categories['{parent_id}']['{category_id}'] = '{category_name}';
    endif;
    ?>

    {/exp:channel:categories}

    {exp:channel:categories category_group="2" style="linear" parent_only="yes"}

    <div id="products-{category_url_title}" class="products-row grayscale">
        <div class="products-cover">
            <div class="page-heading"><h1>{category_name}</h1></div>
        </div>
        <div class="row-content products-content">
            <div class="container">
                <div>
                    <p>{category_description}</p>
                    <div class="white-box">

                        <?php if(isset($categories['{category_id}'])) : 
                        foreach ($categories['{category_id}'] as $category_id => $category_name) : ?>

                        <h3><?= $category_name ?></h3>

                        <ul>
                            {exp:channel:entries channel="products" category="<?= $category_id ?>"}
                            <li><a href="#" data-product="{entry_id}">{title}</a></li>
                            {/exp:channel:entries}
                        </ul>

                        <?php endforeach; endif; ?>

                    </div>
                </div>

            </div>
        </div>
    </div>

    {/exp:channel:categories}

This works fine in that it properly iterates though the children for each parent, but I can't output the channel entries since the parsing stage is set to output, so the category parameter is never set.

If I do extract that channel entries tag to another template, I run into the same issue as I'd need to pass the category id as an embed variable, which would fail for the same reason.

So right now I'm struggling to identify a reasonable solution or a simpler alternative. If possible, I'd like to avoid utilizing third party addons (not that I don't use them, I just like to minimize their use where possible).

EDIT:

I tried using GWcode Categories to replace the child category loop with the following code (replacing the inner PHP), but I get the same result:

{exp:gwcode_categories cat_id="{category_id}" incl_self="no" style="linear"}

    <h3>{cat_name}</h3>

    <ul>
    {exp:channel:entries channel="products" category="{cat_id}"}
        <li><a href="#" data-toggle="modal" data-modal="product" data-target="#product-modal" data-product="{entry_id}">{title}</a></li>
    {/exp:channel:entries}
    </ul>

{/exp:gwcode_categories}
1

Use the parse="inward" parameter.

{exp:gwcode_categories cat_id="{category_id}" incl_self="no" style="linear" parse="inward"}

Which will ensure {cat_id} is set prior to the channel:entries tag being run.

| improve this answer | |
  • I'll give that a go when I get a chance, but I was able to get it working by a combination of using GWcode and extracting the channel entries tag to another template. Your suggestion would naturally just make things simpler, which is always nice. – Bobe Jun 14 '14 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.