Using L&L Inside a Category Archive

@werlecreative we haven’t had a chance to fix/release this, but it’s on the to-do list. I’ll follow up here when this is resolved.

1 Like

Does the most recent version address this issue? 3.1.8?

I’m not sure when the fix was released, but both methods in the solution above are working as expected in version 3.1.8

Hi everyone!

I’d like to go one step further in this topic and output multi-level hierarchical terms in a Taxonomy Archive.

The 3 levels hierarchy:

Parent term
– Child term
–– Grand-child term

The L&L template:

<ul>
<Loop field=archive_term>
  <h3><Field title /> / id<Field id /> (<Field count />)</h3>
  <ul>
  <Loop field=children>
    <li>
      <Field title /> (<Field count />)
      <ul>
      <Loop field=children>
        <li><a href="{Field url}"><Field title /> (<Field count />)</a></li>
      </Loop>
      </ul>
    </li>
  </Loop>
  </ul>  
</Loop>

Output:

Parent Term / idx (x)
– Child Term (x)
–– Grand-child Term (x)
– Grand-child Term (x)

It’s almost fine except the 3rd level ‘Grand-child term’ is returned both in the nested loops 2 and 3.
Is there a way to limit <Loop field=children> to a single terms hierarchy level?
In other words, exclude level 3 Grand-child terms from loop 2 and only return direct children.

Thanks for your help. :wink:

Thanks to ChatGPT, i have a PHP shortcode to achieve this now, as a backup.
I’d be interested to know if it’s possible with L&L though.

2 Likes

Sharing a Solution for Displaying Level N-1 Categories in a Category Page Template

First, I’d like to thank everyone on this forum who shared helpful insights and put me on the right track. I’m not a developer; I’m just someone who enjoys working through logical problems. Using a combination of Tangible’s documentation, the forum, and ChatGPT, I managed to create a solution for displaying only the level N-1 categories in a category page template using a Tangible code template.

The key challenge I encountered was that when retrieving child categories using the children field, it includes all descendants — subcategories, sub-subcategories, and so on. The solution I came up with involved comparing IDs to isolate the direct children of the target category (level N-1). It’s not perfect, and there’s probably a better way to approach it, but this solution works well for my needs.

I also struggled a bit with the If logic syntax, but by testing repeatedly and referencing the documentation, I eventually resolved those issues. ChatGPT was incredibly helpful in generating and refining the logic.

I’m sharing my code here in case it might help others. Hopefully, it can save someone else some time. Thanks again to everyone who contributed on this forum, and good luck to those still working through similar challenges!

<!--
This code is designed to create a category page template using a Full Site Editing (FSE) theme like Twenty Twenty-Five. The goal is to display only the direct child categories (level N-1) of the target category (the current category of the page).

### Background Explanation:
When testing child terms of a category within a loop, the loop retrieves all descendant categories (including subcategories, sub-subcategories, etc.). It does not isolate direct children (level N-1).

To address this, we compare the parent ID of each child term against the target category ID. If the parent ID matches the target category ID, we keep the child. Otherwise, we exclude it, as it indicates the term is a sub-subcategory or deeper.

This approach is ideal for creating automated menus or hierarchical structures, such as tutorials, help systems, or organized information displays, where only direct child categories are relevant.
-->

<Loop field=archive_term>
  <!-- Store the ID of the target category (the current category of the page) -->
  <Set targetCategoryID><Field id /></Set>

  <!-- Display the current category -->
  <h1>Current Category: <Field title /></h1>

  <!-- Check if subcategories exist -->
  <If loop exists field=children>
    <ul>
      <!-- Loop through the subcategories -->
      <Loop field=children>
        <!-- Extract the raw parent ID of the subcategory -->
        <Set childCategoryParentID>
          <Format replace="[" with="">
            <Format replace="]" with="">
              <Field parent />
            </Format>
          </Format>
        </Set>

        <!-- Compare to verify if it is a direct child of the target category -->
        <Set logic="isDirectChild">
          <If variable="targetCategoryID" is value="{Get childCategoryParentID}">true<Else />false</If>
        </Set>

        <!-- Display only if the subcategory is a direct child -->
        <If logic="isDirectChild">
          <li>
            <a href="{Field url}"><Field title /></a>
          </li>
        </If>
      </Loop>
    </ul>
  <Else />
    <p>This category has no subcategories.</p>
  </If>
</Loop>

1 Like