Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How do I add parent to children nav list? WordPress

  • SOLVED

I have had success with my sidebar nav php code that gives me a list of all children of a parent post which I picked up from your site.

Recently you gave me help with a css styling on this code.
question #1234 thanks

My question now:
Is there a way to not only show the children in the sidebar nav but also the parent within the same list as the children.

Thanks

Here is the code I have already

<?php

if($post->post_parent)

/* if this post has a post_parent make a linked list of the children called $children */

$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");

else

/* OR if this post IS a post_parent */

$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");

if ($children) {

/* if $children is not empty, make the navblock */

?>


<ul id="sidebar_nav">


<li class="childnav">

<ul>

<?php echo $children; ?>

</ul>

</li>

</ul>

<?php } ?>




Your help is appreciated!
Nancy

Answers (3)

2010-12-01

idt answers:

Try this:

<?php
$parent_title = get_the_title($post->post_parent);?>
<a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a>
<?php
if($post->post_parent)
/* if this post has a post_parent make a linked list of the children called $children */
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
/* OR if this post IS a post_parent */
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
/* if $children is not empty, make the navblock */
?>
<ul id="sidebar_nav">
<li class="childnav">
<ul>
<?php echo $children; ?>
</ul>
</li>
</ul>

<?php } ?>

That is your original code with the addition of the code to display the parent.

2010-12-01

Nilesh shiragave answers:

If you want to add the Heading within the list then add following lines of code.

<?php
if($post->post_parent)
/* if this post has a post_parent make a linked list of the children called $children */
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
/* OR if this post IS a post_parent */
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
/* if $children is not empty, make the navblock */
?>
<ul id="sidebar_nav">
<li><a href="<?php echo get_permalink($post->post_parent);?>" title="<?php echo get_the_title($post->post_parent);?>"><?php echo get_the_title($post->post_parent);?></a></li>
<li class="childnav">
<ul>
<?php echo $children; ?>
</ul>
</li>
</ul>
<?php } ?>

And if you want to display the title of the parent like heading then add following code

<?php
if($post->post_parent)
{
?>
<a href="<?php echo get_permalink($post->post_parent);?>" title="<?php echo get_the_title($post->post_parent);?>"><?php echo get_the_title($post->post_parent);?></a>
<?php
}
if($post->post_parent)
/* if this post has a post_parent make a linked list of the children called $children */
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
/* OR if this post IS a post_parent */
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
/* if $children is not empty, make the navblock */
?>
<ul id="sidebar_nav">
<li><a href="<?php echo get_permalink($post->post_parent);?>" title="<?php echo get_the_title($post->post_parent);?>"><?php echo get_the_title($post->post_parent);?></a></li>
<li class="childnav">
<ul>
<?php echo $children; ?>
</ul>
</li>
</ul>
<?php } ?>

if you don't want link to the parent page then remove a tag from this code

<a href="<?php echo get_permalink($post->post_parent);?>" title="<?php echo get_the_title($post->post_parent);?>"><?php echo get_the_title($post->post_parent);?></a>


I have similar post on my website where it shows If the current page is a child page then all the child pages of that parent page will be displayed. And if the current page is not a child page and have childs then all the child pages will be displayed of the current page.

http://www.snilesh.com/resources/wordpress/wordpress-tips-and-tricks/wordpress-get-all-subpages-of-current-pages-parent-page/


Nancy Rodger comments:

thank you both id and Nilesh gave answers that worked. I will try to figure out how to divide the winnings.

Thanks again.

2010-11-30

Jim Dugan answers:

Does this help:
http://codex.wordpress.org/Function_Reference/wp_list_pages#List_parent_Page_and_all_descendant_Pages

<?php
// use wp_list_pages to display parent and all child pages all generations (a tree with parent)
$parent = 93;
$args=array(
'child_of' => $parent
);
$pages = get_pages($args);
if ($pages) {
$pageids = array();
foreach ($pages as $page) {
$pageids[]= $page->ID;
}

$args=array(
'title_li' => 'Tree of Parent Page ' . $parent,
'include' => $parent . ',' . implode(",", $pageids)
);
wp_list_pages($args);
}
?>


Nancy Rodger comments:

Not working,
I get a blank sidebar

So I added the code to what I got before.
<?php
/**
* The Sidebar containing the primary sidebar widget areas.
*
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/



if($post->post_parent)



/* if this post has a post_parent make a linked list of the children called $children */



$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");



else



/* OR if this post IS a post_parent */



$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");



if ($children) {



/* if $children is not empty, make the navblock */


// use wp_list_pages to display parent and all child pages all generations (a tree with parent)

$parent = 93;

$args=array(

'child_of' => $parent

);

$pages = get_pages($args);

if ($pages) {

$pageids = array();

foreach ($pages as $page) {

$pageids[]= $page->ID;

}



$args=array(

'title_li' => 'Tree of Parent Page ' . $parent,

'include' => $parent . ',' . implode(",", $pageids)

);

wp_list_pages($args);

}

?>





<ul id="sidebar_nav">





<li class="childnav">



<ul>


<?php echo $parent;?>
<?php echo $children;?>




</ul>



</li>



</ul>



<?php }?>

ass seen here the parent is listed but not as a link and as the number 93.

http://cuyha.com/sidebar-primary/child-sidebar-primary2/

Any ideas greatly appreciated.
I am not a php expert, just poke around.

I think the
$parent = 93;
is suspect