How to get the Parent post in WordPress using PHP

Use can this code snippet in a multitude of ways. I mostly use it inside while loops to get parent data or for breadcrumb navigation.

Here’s a PHP and HTML hybrid solution:

<?php if($post->post_parent) : ?>
<a class="parent-page-title" href="<?php echo get_permalink( $post->post_parent ); ?>"><?php echo get_the_title( $post->post_parent ); ?></a>: 
<?php endif ?>

Pretty simple huh? Here’s a full PHP version:

<?php if($post->post_parent) :
     echo ' <a class="parent-page-title" href=" ' . get_permalink( $post->post_parent ) . ' "> ' . get_the_title( $post->post_parent ) . '</a>';
endif ?>

3 thoughts on “How to get the Parent post in WordPress using PHP

Leave a Reply to Jared Cancel reply

Your email address will not be published. Required fields are marked *