WordPress: get post_parent post

I created a special sidebar that captures parent pages:

query_posts("post_type=page&post_parent=6"); 

I would like to get the name post_parent (i.e. "O"). the_title will not work because this is the title of the child pages.

How can I display the post_parent header?

+7
source share
4 answers

It looks like you already have the parent message id, so you can just use this:

 <?php $parent_post_id = 6; $parent_post = get_post($parent_post_id); $parent_post_title = $parent_post->post_title; echo $parent_post_title; ?> 

(Insert your parent id post in $ parent_post_id)

Link: http://codex.wordpress.org/Function_Reference/get_post

+6
source
 echo get_the_title( $post->post_parent ); 

or

 echo get_the_title( X ); 

Where X is any valid page / page id.

No need to get the full post object for just one property.

+20
source

This is the clean and nice code you need:

It is also retained for use when there is more than one level of parental hierarchy.

 <?php $current = $post->ID; $parent = $post->post_parent; $grandparent_get = get_post($parent); $grandparent = $grandparent_get->post_parent; ?> <?php if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {echo get_the_title($grandparent); }else {echo get_the_title($parent); }?> 
+1
source

I wrote this, it will grab the parent post and then repeat the parent title, etc. Take a look and let me know if this works for you.

https://gist.github.com/1140481

This should work even outside the wordpress loop.

0
source

All Articles