1

I'm wanting to list child pages of my current page in the sidebar of theme. However my sidebar (left side) is before the loop in the template and the post->ID won't return anything if it's before the loop.

My current code:

<?php

wp_list_pages('title_li=&child_of='.$post->ID); 

?>

I've read something about calling a global variable to access this but haven't had any luck thus far.

Any help would be appreciated.

1

... the post->ID won't return anything if it's before the loop

Generally speaking, this is false. The $post variable is set well before the Loop starts in your theme, for most pages. The main query "primes" that variable to the first post in the Loop. That said, depending on this variable outside the Loop is not the most reliable approach. More reliable, would be to use get_queried_object.

$qobj = get_queried_object();
wp_list_pages('title_li=&child_of='.$qobj->ID);

Because get_queried_object returns different information depending on the page you are on, you are also better off checking for the page type before using it.

if (is_page()) {
    $qobj = get_queried_object();
    wp_list_pages('title_li=&child_of='.$qobj->ID);
}

Or check the object itself.

$qobj = get_queried_object();
if (isset($qobj->post_type) && 'page' == $qobj->post_type) {
  wp_list_pages('title_li=&child_of='.$qobj->ID);
}

Those are just examples. I don't know what your precise conditions are.

1

Try this:

// get the post id
$post_id = get_queried_object_id();

// get the children
$children = get_pages('child_of' => $post_id);

//output them
if( count( $children ) != 0 ) {
    echo "<ul>";
    foreach ($children as $child) {
        // if we only want children, not grandchildren
        if ($child->post_parent == $post_id) {
            echo "<li><a href=\"" . get_permalink($child->ID) . "\">";
            echo $child->post_title;
            echo "</a></li>";
        }
    }
    echo "</ul>";
}
2
  • Edited to use get_queried_object_id()
    – crdunst
    May 24 '13 at 13:16
  • Edited again to change the reference to the $post_id variable in the if statement
    – crdunst
    May 24 '13 at 13:23
0

try to make code like

global $post;
var_dump($post);//test values in $post
wp_list_pages('title_li=&child_of='.$post->ID);
6
0

There's really not enough information to give an accurate answer here, but if your suspicion is correct, try replacing $post->ID with get_the_ID().

7
  • Thanks for the reply. I'm happy to give anymore information I'm fairly new to this kind of stuff that's why my question may be a bit specific? - I've just tried replacing $post->ID with get_the_ID() and had no luck
    – rowefx
    May 24 '13 at 13:06
  • How about get_queried_object_id() ? May 24 '13 at 13:08
  • <?php wp_list_pages('title_li=&child_of='.get_queried_object_id()); ?> - this returns all pages as if there was no filter being applied
    – rowefx
    May 24 '13 at 13:12
  • It seems like you're not on a page. Are you actually on a single page view? (rendered with page.php or an overridden page template?) May 24 '13 at 13:15
  • Yes, I'm on my home.php page, though I do want this to appear on every page eventually.
    – rowefx
    May 24 '13 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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