WordPress: get post_parent title WordPress: get post_parent title wordpress wordpress

WordPress: get post_parent title


echo get_the_title( $post->post_parent );

or

echo get_the_title( X );

Where X is any valid post/page ID.

No need to get a complete post object just for one property.


It looks like you've already got the ID of the parent post, 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 post id at $parent_post_id)

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


This is the clean and nice code you need:

It is also save to use when there is more than one parent hierarchy level.

<?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); }?>