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.
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 useget_queried_object
.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.Or check the object itself.
Those are just examples. I don’t know what your precise conditions are.
Try this:
try to make code like
There’s really not enough information to give an accurate answer here, but if your suspicion is correct, try replacing
$post->ID
withget_the_ID()
.