0

I want to output a list of Category A's child categories, then at the end of that list, display a link to Category A itself (.review):

<h1>Category A</h1>

<ul class="readings">
    <li><a href="topics/readings/cat-child-1">Category Child 1</a></li>
    <li><a href="topics/readings/cat-child-2">Category Child 2</a></li>
    <li><a href="topics/readings/cat-child-3">Category Child 3</a></li>
    <li class="review"><a href="topics/review/category-a">Category A Review</a></li>
</ul>

Basically just an upside-down category list. So I did this with GW Code Categories:

{exp:gwcode_categories
    cat_url_title='{segment_2}'
    style='linear'
    show_empty='no'
}
    {if depth2_start}
        <h1>{cat_name}</h1>

        <ul class="readings">
    {/if}

    {if depth == 3}
        <li><a href="{path='topics/readings/{cat_url_title}'}">{cat_name}</a></li>
    {/if}

    {if depth2_end}
        <li class="review"><a href="{path='topics/review/{cat_url_title}'}">{cat_name} Review</a></li>  
        </ul>
    {/if}
{/exp:gwcode_categories}

This almost works perfectly, except for the .review list item at the end. Instead of displaying the URL and name of Category A (depth2), it displays the URL and name of the last child category (depth3).

What am I missing here?

1 Answer 1

1

Just to give you some idea of how this (and other add-ons work), it will loop through each matching category within the gwcode_categories tag pair.

In your example it's looping through 4 times, once for Category A, then for Category Child 1 through to 3.

The logic of your template code is assuming there's a 5th loop which is Category A again closing. This isn't the case, all it is Category Child 3 but with the appropriate variables set to inform the template that not only does this end depth 3, but also depth 2. All the variables will still remain as Category Child 3 - which is why you can't output "{cat_name} Review".

Luckily, GWCode has almost every variable you'll ever need and you want to utilise the parent variables. Change your last piece of code to:

{if depth2_end}
    <li class="review"><a href="{path='topics/review/{parent_url_title}'}">{parent_name} Review</a></li>  
    </ul>
{/if}
1
  • Those dang loops get me every time!
    – kmgdev
    May 7, 2013 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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