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.
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.
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.
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”.
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.
$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
))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With