4

I've asked a question about listing all sub-pages before:

Listing all sub-pages?

But all the questions were wrong.

Anyways, I have now simpler question.

global $post;
echo $post>ID

Works totally fine, but while on pages sidebars only. When it's next to the blog loop (in a blog section) it goes crazy, for example Blog has ID of 216 (and $post>ID in loop.php shows 216), but the $post>ID in the sidebar shows 87. Why is this happening? How to fix that?

Thank you!

[edit]

I have a suspicion that loop and sidebar are both included in index page separately and loop is getting different post ID than sidebar. The big question is how to change that.

[edit for Rarst]

 wp_reset_postdata();

                global $post;

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

Gives the same result. It works with pages, not blog. I have a normal loop, as like in Twenty Ten for example.

7
  • 1
    Hmm... Are you trying to make it work on page with loop of multiple posts? Which post's ID you expect to get?
    – Rarst
    Mar 5, 2011 at 14:31
  • @Rarst, exactly. I have a few PAGES and I want to display all children (sub-pages) of them. So when user visits Blog I want it to display Blog children. So I may be wrong and should go for page ID not post ID? But $page>id displays list of all pages everywhere. Af. Mar 5, 2011 at 14:32
  • 1
    Where are you calling this code, which file, and where in the file? (and, inside a function or just in the file?).
    – t31os
    Mar 5, 2011 at 14:33
  • @t31os, inside a widget that is included in a sidebar. It's inside function widget( $args, $instance ) { } as widgets output. Mar 5, 2011 at 14:34
  • 1
    Assuming you're not using query_posts to perform other queries somewhere between when the main loop occurs and when the sidebar gets output, $wp_query->post should hold an object of the first post(or page/custom type) from the main query, you could use that to determine ID, eg. $wp_query->post->ID.
    – t31os
    Mar 5, 2011 at 15:17

2 Answers 2

5

If i understand correctly you are trying to display a list of child pages of a page in a widget, if so then first check if you are on a page using the conditional tag is_page() then you can use $wp_query->get_queried_object_id() like t31os has pointed out so your widget display function should look like this:

if (is_page()){
    Global $wp_query;
    $current_page_id = $wp_query->get_queried_object_id();
    $children = wp_list_pages('title_li=&child_of='.$current_page_id.'&echo=0');
    echo $children;
}

So only if you are on a page this code will run

3
  • your code works fine but displays nothing on "Blog". So the effect is almost the same as with wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); . I want to display all sub pages on EVERY page, including the blog one! :) Mar 5, 2011 at 16:29
  • 1
    there you go , that means that your bolg is not a page but probably a category, in that case you don't need child pages , you need posts, in that category.
    – Bainternet
    Mar 5, 2011 at 16:38
  • oh my God, you're a genius and I am a dumba... ;) Mar 5, 2011 at 22:02
5

$post changes every time the_post() or setup_postdata() are used - which is in most of the loops.

Use wp_reset_postdata() to kick it back to original data.

Edit

$post holds data of individual post, set up during the Loop. It makes no sense to use it out of that context.

To process multiple pages you need to query for them (with get_pages() for example) and work with that.

1
  • check the last edit :( Mar 5, 2011 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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