# List Child Items of a Hierarchical Post Type

> Adds a [child_items] shortcode that lists the children of the current page or hierarchical item, such as sub-services under a service.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: hierarchy, shortcode, navigation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/child-items-shortcode/

## Code

```php
add_shortcode( 'child_items', function ( $atts ) {
    $atts = shortcode_atts( array( 'excerpt' => 'no' ), $atts, 'child_items' );
    $post = get_post();

    if ( ! $post || ! is_post_type_hierarchical( $post->post_type ) ) {
        return '';
    }

    $children = get_posts( array(
        'post_type'      => $post->post_type,
        'post_parent'    => $post->ID,
        'posts_per_page' => 100,
        'orderby'        => array( 'menu_order' => 'ASC', 'title' => 'ASC' ),
        'no_found_rows'  => true,
    ) );

    if ( ! $children ) {
        return '';
    }

    $html = '<ul class="child-items">';

    foreach ( $children as $child ) {
        $html .= '<li><a href="' . esc_url( get_permalink( $child ) ) . '">' . esc_html( get_the_title( $child ) ) . '</a>'
            . ( 'yes' === $atts['excerpt'] && has_excerpt( $child ) ? '<br><span class="child-excerpt">' . esc_html( get_the_excerpt( $child ) ) . '</span>' : '' ) . '</li>';
    }

    return $html . '</ul>';
} );
```

## Notes

Ordered by the Order field, then title. Add excerpt="yes" to include short descriptions.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "List Child Items of a Hierarchical Post Type" 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/
