Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve a list of a Wordpress page's sibling pages?

I am trying to create a list of sibling pages (not posts) in WordPress to populate a page's sidebar. The code I've written successfully returns a page's parent's title.

<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title; ?>

As I understand it, you need a page's id (rather than title) to retrieve a page's siblings (via wp_list_pages). How can I get the page's parent's id?

Alternate approaches are welcome. The goal is to list a page's siblings, not necessarily just retrieving the parent's id.

like image 826
sjstrutt Avatar asked Feb 23 '10 22:02

sjstrutt


People also ask

How do I show a list of child pages in a parent page in WordPress?

add_shortcode( 'wpb_childpages' , 'wpb_list_child_pages' ); The code above first checks to see if a page has a parent or the page itself is a parent. If it is a parent page, then it displays the child pages associated with it. If it is a child page, then it displays all other child pages of its parent page.

How do I list all pages in WordPress?

Name the title something suitable like “Sitemap” or “Website Directory” and then paste the shortcode [pagelist] into the widget. Click on the “Save” button to finish. You can now view the Sitemap on your website's sidebar. Congratulations, you have successfully added a page list to your WordPress website.

How do parent pages work in WordPress?

A parent page is a top-level page, with child pages nested under it. For example, you could have an “About” page as a top level or parent page, and then have child pages “Life Story” and “My Dogs” under it. Under “My Dogs” you could have another page, titled “Rosco”.

How do I add a parent page in WordPress?

On your WordPress dashboard, go to Pages -> Add New. Enter the desired title and content. Open the Page tab and scroll down to Page Attributes. Choose a specific page to set as the parent from the drop-down menu.


2 Answers

$post->post_parent is giving you the parent ID, $post->ID will give you the current page ID. So, the following will list a page's siblings:

wp_list_pages(array(
    'child_of' => $post->post_parent,
    'exclude' => $post->ID
))
like image 185
Richard M Avatar answered Nov 01 '22 18:11

Richard M


wp_list_pages(array(
    'child_of' => $post->post_parent,
    'exclude' => $post->ID,
    'depth' => 1
));

The correct answer, as both other answers don't exclusively display the siblings.

like image 32
pbond Avatar answered Nov 01 '22 19:11

pbond