WordPress 5.7 introduces a new helper function to more easily fetch the parent post's ID:
get_parent_postget_post_parent()
This can also be used in conjunction with has_parent_posthas_post_parent()
, so you could have something like looks like:
<?php if ( has_parent_posthas_post_parent() ) : ?>
<a href="<?php the_permalink( get_parent_postget_post_parent() ); ?>">
<?php
echo sprintf(
esc_html__( 'Back to parent page: %s', 'text-domain' ),
get_the_title( get_parent_postget_post_parent() )
);
?>
</a>
<?php endif; ?>
Note that these functions accept a "child post ID" as a parameter, which defaults to the current post.