How to List Subpages and Siblings on Your WordPress Site

Home » Snippets » How to List Subpages and Siblings on Your WordPress Site
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Organizing your website’s content carefully and coherently is crucial for effective navigation. In WordPress, one great way of managing content is by creating parent, child, and sibling pages. However, listing subpages and sibling pages of a given page can be a challenging task for some. In this article, we will guide you through a straightforward process of listing subpages and siblings in WordPress, helping you curate your website more efficiently.

					function wpturbo_list_subpages_siblings() {
    global $post;
    
    if ( $post->post_parent ) {
        $children = wp_list_pages( array(
            'title_li'  => '',
            'child_of'  => $post->post_parent,
            'echo'      => 0
        ) );
    } else {
        $children = wp_list_pages( array(
            'title_li'  => '',
            'child_of'  => $post->ID,
            'echo'    => 0
        ) );
    }

    if ( $children ) {
        echo '<ul>';
        echo $children;
        echo '</ul>';
    }
}

add_action( 'wp_list_pages', 'wpturbo_list_subpages_siblings' );
				

The code snippet is divided into two main parts: the function definition and a hook that calls the function.

The function wpturbo_list_subpages_siblings() starts by calling the global $post variable. This variable is an instance of the WP_Post class and contains the post object’s information that WordPress is currently dealing with, or in simple terms, it holds the information about the current post.

The first if condition checks if $post->post_parent is set. The $post->post_parent represents the parent post’s ID from which the current post is descended. If this condition is true, it means our current post is a child or subpage.

We use the wp_list_pages() function to obtain a list of pages. This function lists the pages of your WordPress website. We pass an array to this function which includes three parameters:

  • title_li : an empty string because we don’t wish to have a title for the list
  • child_of : the parent page’s ID to ensure the function fetches only the child pages of the current post. We use $post->post_parent for this parameter to specify that we only want pages that are children of the parent of the current post.
  • echo : set to 0 which means the list of pages will not be displayed straight away but returned as a string.

This list is stored in the $children variable.

If $post->post_parent is not set, meaning the page is a parent page, the same wp_list_pages() function is called. But this time, for the child_of parameter, it passes $post->ID, indicating that we only want pages that are children of the current page.

Finally, if $children (the lists of child pages) exists, it gets wrapped inside unordered list tags (<ul>) and echoed on the webpage. This block of code will output subpages if the current post is a parent, and sibling pages if the current post is a child.

After the function definition, we have the add_action() function. This function allows us to ‘hook’ our defined function into a specific action, which is wp_list_pages in this case. With this hook, our wpturbo_list_subpages_siblings() function will be called each time wp_list_pages action is triggered in WordPress, resulting in the display of either subpages or sibling pages.

Register an account to save your snippets or go Pro to get more features.