# Subcategory Links at the Top of Category Archives

> Shows a row of links to the child categories at the top of a category archive, helping visitors drill down into large sections.

- Type: PHP
- Runs: Frontend only
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: categories, navigation, archives
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/subcategory-links-on-category-archives/

## Code

```php
add_action( 'loop_start', function ( $query ) {
    static $done = false;

    if ( $done || ! $query->is_main_query() || ! is_category() || is_paged() ) {
        return;
    }

    $done     = true;
    $children = get_categories( array(
        'parent'     => get_queried_object_id(),
        'hide_empty' => true,
    ) );

    if ( ! $children ) {
        return;
    }

    echo '<nav class="wpide-subcategories" aria-label="Subcategories"><ul style="list-style:none;margin:0 0 2em;padding:0;display:flex;flex-wrap:wrap;gap:.5em;">';

    foreach ( $children as $child ) {
        echo '<li><a href="' . esc_url( get_category_link( $child ) ) . '" style="display:inline-block;padding:.3em .8em;border:1px solid currentColor;border-radius:999px;text-decoration:none;">' . esc_html( $child->name ) . ' <span style="opacity:.6;">' . esc_html( number_format_i18n( $child->count ) ) . '</span></a></li>';
    }

    echo '</ul></nav>';
} );
```

## Notes

Only categories with posts are listed. The position depends on where your theme starts the post loop.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Subcategory Links at the Top of Category Archives" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
