Wordpress Display Current page and parent/children - php

I'm trying to create a sidebar navigation that has the current page and it's children displayed (or if it's the child page, it's siblings and the parent page)
I can only get the children or, when on a child, siblings to display.
How can I get
Parent
Child
Child
To display when on either the parent or child Pages? I don't want the parent page's siblings to display and nothing I try seems to work!
Thanks
<?php
//GET CHILD PAGES IF THERE ARE ANY
$children = get_pages('child_of='.$post->ID);
//GET PARENT PAGE IF THERE IS ONE
$parent = $post->post_parent;
//DO WE HAVE SIBLINGS?
$siblings = get_pages('child_of='.$parent);
if( count($children) != 0) {
$args = array(
'depth' => 1,
'title_li' => '',
'child_of' => $post->ID
);
} elseif($parent != 0) {
$args = array(
'depth' => 1,
'title_li' => '',
'child_of' => $parent
);
}
//Show pages if this page has more than one sibling
// and if it has children
if(count($siblings) > 1 && !is_null($args))
{?>
<ul class="pages-list">
<?php wp_list_pages($args); ?>
</ul>
<?php } ?>

Related

WordPress Side Navigation with Parent Heading and Child Sub Pages

I have the following navigational structure in WordPress:
Page
Sub Page
Child Page
Child Page
Child Page
Page
Sub Page
Child Page
Child Page
Child Page
Page
Sub Page
Child Page
Child Page
Child Page
On each Sub Page and Child Page, I want to display the following in the sidebar:
Sub Page
Child Page
Child Page
Child Page
The closest I've come to this is the following code from here:
<ul>
<?php
global $post;
$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
wp_list_pages( array(
'title_li' => '',
'child_of' => $current_page_parent,
'depth' => '1' )
);
?>
</ul>
However, on Sub Pages it only shows a list of all Sub Pages (without Child Pages) and on Child Pages, it displays a list of Child Pages of that section but the parent Sub Page title is missing.
How do I modify to achieve what I'm looking for?
Thanks!
Yes, you are right this is not showing parent page on child or sub for that you may use following code.
<?php if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0
) );
$title = get_the_title( $post->post_parent ); } else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0
) );
$title = get_the_title( $post->ID );} if ( $children ) : ?>
<h2><?php echo $title; ?></h2>
<ul>
<?php echo $children; ?>
</ul>
Mukesh Ram's code is working, just missing the final ENDIF
<?php if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0
) );
$title = get_the_title( $post->post_parent ); } else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0
) );
$title = get_the_title( $post->ID );} if ( $children ) : ?>
<h2><?php echo $title; ?></h2>
<ul>
<?php echo $children; ?>
</ul>
<?php endif; ?>

List a child's sibling pages in Wordpress

I'm build a website for a hotel and in my sidebar file I have a PHP script to print out the list of child pages. The structure is as follows:
Restaurant (Breakfast, Lunch, Dinner)
Rooms (Standard, Double, Luxury)
So, essentially I want to display the child (ones in the brackets) pages in the sidebar when a user is on the parent page (Restaurant or Rooms) or a child page.
However, my script doesn't appear to work and doesn't display the list of pages.
//SET UP FAMILY
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$siblings = get_pages('child_of='.$parent);
if(count($children) != 0 ) {
$args = array(
'depth' => 1,
'title_li' => null,
'sort_column' => 'menu_order',
'child_of' => $post->ID
);
}
else if($parent != 0) {
$args = array(
'depth' => 1,
'title_li' => null,
'sort_column' => 'menu_order',
'exclude' => $post->ID,
'child_of' => $parent
);
}
if(count($siblings) > 1 && !is_null($args)) {
echo "<ul class='pages'>";
echo "<h2>Browse</h2>";
wp_list_pages($args);
echo "</ul>";
}
I don't mind if the current page is shown in the menu either.

Show title only if content

I want to hide <h2>title</h2> if they are not children pages.
<h2> title</h2></br>
<?php
// Globalize the $post variable;
// probably already available in this context, but just in case...
global $post;
wp_list_pages( array(
'title_li' => __(''),
// Only pages that are children of the current page
'child_of' => $post->ID,
// Only show one level of hierarchy
'depth' => 1,
) );
?>
You can use the get_children function to get all child contents of your post. If the return array is empty, there are no child pages defined:
<?php
$children =& get_children( 'post_type=page' );
if (!empty($children)) "<h2> title</h2>";
?>
Documentation:
get_children:
https://codex.wordpress.org/Function_Reference/get_children
Post types: https://codex.wordpress.org/Post_Types

Wordpress navigation for grandchildren

I'm currently using this code (per the codex) to show children on parent pages, and children of the parent pages on their children:
<?php if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
I would like to add that if on a secondary child page (child's child) to then show it's parent and parent's siblings.
Thank you for your help! :D
<?php
if($post->post_parent)
{
//get the parent post
$parent = get_post($post->post_parent);
//check to see if we have a grandparent
if($parent->post_parent)
{
$page_list = wp_list_pages( array( 'child_of' => $parent->post_parent, 'echo' => false, 'depth' => 1 ) );
}
else
{
$page_list = wp_list_pages( array( 'child_of' => $post->post_parent, 'echo' => false, 'depth' => 1 ) );
}
}
else
$page_list = wp_list_pages( array( 'child_of' => $post->ID, 'echo' => false, 'depth' => 1 ) );
if ($page_list) {
?>
<ul>
<?php echo $page_list; ?>
</ul>
<?php } ?>
This will check if the post has a parent and then if that post has a parent. The $page_list should be a list of pages of the parent and its siblings. the 'depth' => 1 tells WordPress to only get one level of pages. This will stop it from getting the children of those pages

retrieve only sub-pages

I want to list all sub-pages only one level though of one particular page. I was reading Function Reference/get pages and thought that $pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ; will do the trick but it is not working. It lists all pages on the same level like the page I call that code from. If I omit parent option I will get all pages even with sub-pages that I want. But I want only sub-pages.
The whole function is like
function about_menu(){
if (is_page('about')){
$pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ;
foreach($pages as $page)
{
?>
<h2><?php echo $page->post_title ?></h2>
<?php
}
}
}
below are screen shots from wp admin and results. Mine is second one
Screen shot from WP admin http://img687.imageshack.us/img687/6139/e2568e8ec2684e7aa1bb3d1.png and the result http://img269.imageshack.us/img269/2365/329c5097c78f4d3186a177c.png
Check out wp_list_pages(). I think these settings will give you what you want.
<?php
$args = array(
'child_of' => $post->ID, // current page
'depth' => 1, // get children only, not grandchildren
'echo' => 1, // print immediately when we call wp_list_pages
'sort_column' => 'menu_order', // sort by the menu_order parameter
);
?>
<?php wp_list_pages( $args ); ?>
try adding
global $post;
right before you declare $pages.
$paginas = array();
$paginas_obj = get_pages('sort_column=post_parent,menu_order');
$paginas['false'] = "Seleciona pagina";
foreach ($paginas_obj as $pagina) {
$paginas[$pagina->ID] = $pagina->post_title;
}

Categories