Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to get displayed child pages on parent page and also on child page of that parent in sidebar?Trying to paste code from wp codex page and from several other sites, but with no success.I always get instead of list of child pages, list of parent pages!? what is the problem?Just to mention, maybe is important, all parent pages have its own page-template.

example code:

    <?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 also try this code which seems to me doing god job, but again I got list of parent pages

share|improve this question
1  
assuming that the page templates are having a custom query, have you restored the original querystring by adding wp_reset_query(); after the custom loop? you can check if the sidebar gets the page ID by adding this before your code: <?php echo $post; ?> –  Michael Jul 14 '11 at 11:26
    
also try to add global $post; before your code, considering you are using it in a textwidget with the 'exec php' plugin. –  Michael Jul 14 '11 at 14:51

4 Answers 4

If you don't want a list of Pages, then why are you calling wp_list_pages() in your code? that function returns a list of Pages.

You probably need to use get_posts() instead, and then loop through the results to output whatever $post content you want to display.

e.g. you could do something like the following:

<?php
global $post;
$child_pages = get_posts( array(
    'post_parent' => $post->ID
) );
?>
<ul>
<?php
foreach ( $child_pages as $child ) {
    ?>
    <li>
        <h3><?php echo $child->post_title; ?></h3>
        <div><?php echo $child->post_content; ?></div>
    </li>
    <?php
}
?>
</ul>

EDIT

If you need to access $post->ID from outside the Loop, do something like this inside the Loop:

$current_post_id = $post->post_ID;

Then, just use $current_post_id outside the Loop. (Note that you'll only want to do this on template pages that display single Posts; otherwise, the value of $current_post_id will change on every cycle of the foreach loop.)

You could then change your get_posts() call accordingly:

$child_pages = get_posts( array(
    'post_parent' => $current_post_id
) );
share|improve this answer
    
That gives me no output at all.Blank... –  user4490 Jul 14 '11 at 14:22
    
It is example code, that needs to be adapted to your specific use case. If you're outside the Loop, you may need to set a variable inside the Loop, to hold $post->ID, so that you can access it outside the Loop. –  Chip Bennett Jul 14 '11 at 14:28
    
yes, Im ouside the loop, text widget with exec plugin.My needs is to display childpages on specific parent page, and also on childpages from current parent page in sidebar.To set variable inside a loop for this specific case is too much for my level of php and wp.Can you help me little closer on this? tnx –  user4490 Jul 14 '11 at 14:39
    
See edited answer. –  Chip Bennett Jul 14 '11 at 14:44
    
I got something, but now Im confused, I put variable inside the loop of my one page template file, but now I got displayed some post with content instead of childpages titles.I got this on every page same also, no metter that Im put variable in just one page template.I got several different page templates for my Pages? –  user4490 Jul 14 '11 at 14:58

this worked for me: notice the use of depth in the child wp_list_pages.

<?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=&depth=1&child_of=".$post->ID."&echo=0");
    if ($children) { ?>

   <ul>
        <?php echo $children; ?>
    </ul>
<?php } ?>
share|improve this answer

This is the solution I am using

<?php
   if ($post->post_parent)  {
        $ancestors=get_post_ancestors($post->ID);
        $root=count($ancestors)-1;
        $parent = $ancestors[$root];
    } else {
        $parent = $post->ID;
    }
    $children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0");
    if ($children) { ?>
    <ul class="tabs vertical hide-on-phones three columns">
        <li>Hekki</li>
        <?php echo $children; ?>
    </ul>
<?php } ?>

I want the parent page to display as well.

share|improve this answer

Try this

    <ul class="sidenav">
    <?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 } ?>          
    </ul>
share|improve this answer
    
its code from my question?? –  user4490 Jul 14 '11 at 11:15
    
If you look closely, it's slightly different. I use this snippet quite often and it shows parent + child pages on parent page + child page of that parent. –  Nathan Fitzgerald - Fitzgenius Jul 14 '11 at 11:16
    
Same output:-(->list of parent pages?huh I put this in sidebar widget, and all my pages have its own page template, just to notice.Maybe is important, Im pretty stack in this issue... –  user4490 Jul 14 '11 at 11:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.