I am looking for reverse functionality.
I have a page by name ‘Parent Page’ and there are 5 child pages say ‘Page A’,’Page B’,’Page C’,’Page D’,’Page E’ which is under ‘Parent Page’. I want to get the parent page name listes when I am within any child page. I tried many and finally decided to write a custom code in sidebar.php
<?php
$post_obj = $wp_query->get_queried_object();
$post_ID = $post_obj->ID;
$post_title = $post_obj->post_title;
$post_name = $post_obj->post_name;
$post_ParentPage = $post_obj->post_parent;
?>
<?php
$querystr = “SELECT post_parent from wp_posts where ID=”.$post_ID;
$pageposts = $wpdb->get_results($querystr, OBJECT);
echo ‘
##’.$post_parent.’##’;
?>
The problem is that the $post_parent always displays null. Can someone help me on this?
sudhishchemmur… you can’t really ‘echo’ an object, which is what you’ve asked wpdb to return. Try print_r($post_parent)
and see what you get.
Possibly, what you want to do is echo $post_parent->ID
.
Alternately, you could use $wpdb->get_var
which will return a plain vanilla variable rather than an array or an object. If you use get_var you should be able to just echo it out.
Try this:
<?php
if($post->post_parent)
$children = wp_list_pages("sort_column=ID&depth=1&title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("sort_column=ID&depth=1&title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul class="child"><?php echo $children; ?></ul>
<?php } ?>
@kapiljain.in
its a nice code snippet, what I was searching for, and just I got it. thanks!
BTW, if possible an enhancement can be done as if the chilled pages not exist better not to display the title(parent page name).
Hello!!
i think this is along the lines of what i have to do.
in plain English:
I need to SELECT the posts that have the same term_id and parent_id as the one being displayed.
how would this be created in SQL?
this would also need to be dynamic, with no category ids hardcoded….
please i hope somebody can help me with this! i have spent to many hours trying to figure it out with no luck 🙁
thank you in advance….