List All Child Sub-Pages with a Shortcode

Cleanshot 2023 04 06 At 16.18.55@2x

Let’s say you have a Page in your WordPress site, and you’ve added a few child pages to it. Sometimes you might wanna list those child pages easily with a shortcode. This creates that shortcode so you can use:  [list_child_pages class="custom-class" parent="parentID"]

For example, you have an “Our Policies” page and below it are some other pages like: Cookie Policy, Privacy Policy, Accessibility Statement, etc.

CleanShot 2023-04-06 at 16.18.55@2x

And you want to display them as a list of pages like this:

CleanShot 2023-04-06 at 16.23.20@2x

or place them in a footer like this:

CleanShot 2023-04-06 at 16.24.51@2x

Well this shortcode let’s you do that. Add this snippet to your site, then you can place the shortcode [list_child_pages] anywhere. By default, it will list the child pages of the current page, but if you’re using it in a menu, header, footer, or sidebar, you might want to give it the ID of the parent page so that it always lists the correct pages.

What it does…

Adds a shortcode that can output a list of child pages for the current or a given page.

Why it does it…

Sometimes you want to easily place a list of child pages, so… do it!

How it does it…

This code defines a custom shortcode for WordPress that allows the user to display a list of child pages on a parent page or any other page specified by the user.

The shortcode is defined as [list_child_pages] and can accept two parameters:

  • parent: The ID of the parent page whose child pages should be listed. If this parameter is not provided, the shortcode will display the child pages of the current page.
  • class: A custom class that can be added to the <ul> element that contains the child page links.

The code creates a PHP class ListChildPagesShortcode that contains a constructor method and an init method. The init method registers the shortcode by calling the add_shortcode function provided by WordPress. It registers the shortcode with the name list_child_pages and specifies the get_list_of_child_pages method of the ListChildPagesShortcode class as the callback function for the shortcode.

The get_list_of_child_pages method first checks whether the parent parameter is provided in the shortcode attributes. If it is, it uses that as the parent ID for the child pages. Otherwise, it uses the ID of the current page as the parent ID.

The method then uses the wp_list_pages function provided by WordPress to retrieve the list of child pages for the specified parent page. It uses the echo parameter set to 0 to capture the output of the function instead of displaying it immediately.

Next, the code sanitizes the class attribute provided by the user and adds it to the <ul> element that contains the child page links.

Finally, the method returns the HTML output containing the <ul> element with the child page links.

Use the JSON file for easily importing into your CodeSnippetsPro plugin. Use the PHP file or just copy/paste the code sample into your functions.php if you’re not using CodeSnippetsPro.

Related Snips

Leave a Reply

Your email address will not be published. Required fields are marked *