Welcome to Genhike Sports

Category Tree

← Back to the tag reference

Hand-written Includes in-depth notes

Category Tree category_tree Used inside components 7 host files / 7 calls

Returns the whole category tree as a nested array for custom navigation, sitemaps, and footer category blocks.

category_tree($lang = null, $onlyVisible = true)

Pages List pages Detail pages

Used in
List pages / Detail pages
Defined in
core/tpl_functions.php:539
Core method
Category::tree()
How do I read these repository paths?

All paths are relative to the site root. Open core/tpl_functions.php at line 539 to see the definition of category_tree(). The "core method" is what the tag actually forwards to: a template writes content_list(...) and the core runs Tpl::contentList(...).

1. Fill in parameters, get the code Edit any field and the result below updates instantly; press Copy and paste it into a template. It also works without JavaScript: press Generate and the server recomputes it.

click the title to collapse / expand
Language code such as en; unknown codes just return an empty array.
True keeps only enabled, unhidden categories; false also returns disabled and hidden ones.
Generated code

Now generating: category_tree

Real page previewOpen in new window

This is the real rendering of the page (real header, container, this tag output, real footer). It reloads when you change a parameter.

This is the server-computed default; it updates as you edit any parameter and the preview on the right is re-rendered too. It also works without JavaScript: fill the form and press Generate.

2. Full parameter table 2 parameters, one per row; scroll sideways on narrow screens.

click the title to collapse / expand
Parameter Type Default Description
lang string Current language Language code such as en; unknown codes just return an empty array.
onlyVisible bool true True keeps only enabled, unhidden categories; false also returns disabled and hidden ones.

3. Examples Every example can be copied as-is; the later ones are more advanced.

click the title to collapse / expand

Minimal

Fetch the tree once, loop once, and take both the name and the URL from the admin data.

<?php foreach (category_tree() as $cat): ?>
    <a href="<?= e(category_url($cat)) ?>"><?= e($cat['name']) ?></a>
<?php endforeach; ?>

Common: footer columns with a second level

One column per top-level category, with children rendered only when they exist.

<div class="ry-footer-nav">
<?php foreach (category_tree() as $cat): ?>
    <div class="ry-footer-nav__col">
        <a class="ry-footer-nav__title" href="<?= e(category_url($cat)) ?>"><?= e($cat['name']) ?></a>
        <?php if ($cat['children']): ?>
        <ul>
            <?php foreach ($cat['children'] as $child): ?>
            <li><a href="<?= e(category_url($child)) ?>"><?= e($child['name']) ?></a></li>
            <?php endforeach; ?>
        </ul>
        <?php endif; ?>
    </div>
<?php endforeach; ?>
</div>

Advanced: only categories flagged for navigation

Nodes expose is_nav, so filtering on it keeps a custom block as selective as the main navigation.

<?php foreach (category_tree() as $cat): ?>
    <?php if (isset($cat['is_nav']) && (int)$cat['is_nav'] !== 1) { continue; } ?>
    <li>
        <a href="<?= e(category_url($cat)) ?>"><?= e($cat['name']) ?></a>
        <?php if ($cat['children']): ?>
        <ul>
            <?php foreach ($cat['children'] as $child): ?>
            <?php if (isset($child['is_nav']) && (int)$child['is_nav'] !== 1) { continue; } ?>
            <li><a href="<?= e(category_url($child)) ?>"><?= e($child['name']) ?></a></li>
            <?php endforeach; ?>
        </ul>
        <?php endif; ?>
    </li>
<?php endforeach; ?>

Advanced: read the tree of another language

Categories are separate records per language, so the names come back translated and the URLs carry the /en/ prefix.

<?php foreach (category_tree('en') as $cat): ?>
    <a hreflang="en" href="<?= e(category_url($cat)) ?>"><?= e($cat['name']) ?></a>
<?php endforeach; ?>

4. What it does / when to use it

Use it to fetch the whole category tree in one call when you build the markup yourself: multi-column footers, HTML sitemaps, custom drop-downs and category pickers. It returns a nested array whose top-level nodes carry a children key, so you can walk it level by level. For the main navigation use nav_menu(), which highlights the active category and honours the admin navigation flag, and for the children of the current category use content_query().

5. Which pages it belongs on This is the question clients actually ask: "I am building the home page / a list page / a detail page — which tags do I use?"

click the title to collapse / expand
Page What that page is What this tag does there
List pages Category list pages: listing, filters, pagination On a list page it lives in the list container or right next to the pagination bar.
You use it to: Use it to fetch the whole category tree in one call when you build the markup yourself: multi-column footers, HTML sitem
Detail pages Content detail pages: body, specs, related items On a detail page it goes in the body area or in the related-items sidebar.
You use it to: Use it to fetch the whole category tree in one call when you build the markup yourself: multi-column footers, HTML sitem

Clicking a page name jumps to every tag for that page in the reference (the "By page" filter there). This page always shows everything about the tag, whatever that filter is set to.

6. How to use it (step by step)

  1. Fetch the tree into a variable and print it once with print_r($tree) to see the available keys on a node: name, slug, id, lang, is_nav and children. Every later step builds on those keys.
  2. Write a single-level loop first and confirm the top-level categories and their order. The order is the admin order, sorted by path and sort order, so never re-sort it by name.
  3. Build URLs with category_url($cat). The url key on a node is the custom link field and is empty for ordinary categories.
  4. Walk $cat['children'] inside the loop when you need a second level. The children key always exists and is an empty array when there are no children, so no key check is needed.
  5. Keep the depth in check: two levels look fine on desktop, while three or more crowd together on phones, so most footers stop at two.
  6. Pass the first argument to read another language, as in category_tree('en'), and pass false only when you really need hidden and unpublished categories, such as in an admin preview or a migration check.

7. Common pitfalls

  • A single-level loop makes every child category vanish, which looks like half the categories are missing and raises no error. Walk $cat['children'] explicitly when you need the second level.
  • Printing the url key of a node as a link yields href="" for ordinary categories, because that column is empty, and clicking such a link just reloads the page. Always use category_url($cat) instead.
  • Echoing the return value as if it were HTML, as in &lt;?= category_tree() ?&gt;, prints the word Array and writes a PHP warning. The tag returns data and you must loop over it.
  • onlyVisible defaults to true, so categories whose status is not 1 or whose is_hidden is 1 are absent from the tree. When a category disappears, check its admin state before touching the template.
  • The tree covers all top-level categories and ignores the include-in-navigation flag. Use nav_menu() when only the flagged categories should appear, or filter on the is_nav key of each node.

Note from the reference table The old advice to use content_query() for child categories is wrong: that function returns content items, not categories. To get one category's children, find the node by id in the returned tree and read its children key; category_tree() always starts at the root (parentId is hard-coded to 0 in the source), so there is no subtree call. Mind the argument order too: onlyVisible is second, and category_tree(false) is read as a language and falls back to the current one.

8. Real rendered output This section was rendered once on the server with this site real data; it is not a mock-up.

click the title to collapse / expand

It outputs a whole page region (this site header / footer / a full layout section, or it decides what to render from the current page data). Inside this small box it would just splice another page skeleton into this one: you would not see its real effect, and it would wreck this page. Copy it into your template instead.

9. Where it already appears in your templates Grouped by components / page templates / template packs; the data comes from a script scan.

click the title to collapse / expand

Components 7 files / 7 calls 89% of tag calls live in components

  • templates/_shared/components/list-products-showcase.php ×1 lines 219
  • templates/_shared/components/sidebar-case.php ×1 lines 144
  • templates/_shared/components/sidebar-classic.php ×1 lines 86
  • templates/_shared/components/sidebar-faq-list.php ×1 lines 62
  • templates/_shared/components/sidebar-news.php ×1 lines 129
  • templates/_shared/components/sidebar-product.php ×1 lines 140
  • templates/_shared/components/sidebar-rank.php ×1 lines 47

Click "See this code + preview": the left shows the real code around that call site (±12 lines, call line highlighted) and the right renders a real page preview with the current parameters.

11. Previous / next In group order; this is tag 47 of 56.

click the title to collapse / expand
Send an Enquiry