Support » Fixing WordPress » Dynamic return of page_id?

  • Hi. I’m trying to use:
    <?php wp_list_pages('child_of=2&title_li='); ?>

    But instead of ‘child_of=2’ I want to return the id of the current page. That way, this will only list the child pages of the page currently being viewed.

    What string do I have to use to get this to work? I can’t figure out the answer from the forums or codex… 🙁

Viewing 15 replies - 1 through 15 (of 17 total)
  • Try this:

    <?php wp_list_pages("child_of=$page_id&title_li="); ?>

    Nope… when I try that I get the following:

    Warning: Invalid argument supplied for foreach() in path/to/my/install/wp-includes/template-functions-post.php on line 378

    However, that is what I’m trying to do. I just don’t know the PHP syntax for it. Any more suggestions?


    It should work. How did you add the code?
    Post the whole file here

    And here’s the part that sucks…. here’s how I ended up solving the issue: code in pastebin/

    Basicaly I had to loop through all my pages until I found the one that matched by using is_page(). Then I pass that ID to the wp_list_pages…. there’s a catch though, it doesn’t properly check to see if there are any child pages to the current one (although it should, I’ll have to work on that.)

    You can see how it does work at my site.


    Thanks, TechGnome. alphaoide, I pasted the code in exactly where I had the bit that I posted in my first message here. While the bit that I posted always returns the posts that are the child of page 2, your bit always returns the error that I posted above.

    TechGnome, where do you have that code inserted? Does it have to be outside of the loop? I’m using a modified version of the Connections theme, and I had it in post.php. It returns an error that there’s an unexpected $ when I put it where I originally had my bit of code.

    ccheaton, I want you to paste the whole file where that snippet of code goes.

    Ok… is this right?

    Ummm mine is in the side bar…. it’s for listing child pages of the current page (nothing to do with posts). And is outside the loop.


    I’ve modified your code to take affect only when viewing a Page.

    Ok, well, now I only get the error message when I’m displaying a page. What I’m trying to do is to have a list of the sub-pages of a particular page appear when that page is viewed. Ideally, it would appear after the content of the page — perhaps at the bottom. However, having it appear in the loop would be good, too.

    I get the same error, but just on pages. You can look here:

    Notice that there is a post on the homepage, but that the About Us link is to a page. I pasted the code into the post.php exactly as it appeared in the link from above…

    Suggestions? Do I have to use PHP to echo the page_id into the statement? Somehow it’s just not working.

    I should add that it is WP 1.5.2

    Try moving line 8-10 to outside The Loop, then. The Loop is probably in index.php. So, put it after The Loop in index.php.

    Hmm… same problem. It’s now outside of the loop and doing the same thing.

    That’s weird because the behavior you are trying to get is exactly what that code does. It could be that I am a little behind in the code base (shhh! don’t tell anyone.) so it may be a core versioning difference….. dang, now yo uhave me concerned about it…


Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Dynamic return of page_id?’ is closed to new replies.