Skip to main content
Updated to reflect @markdwhite's comment about changing function names.
Source Link

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.

https://make.wordpress.org/core/2021/02/10/introducing-new-post-parent-related-functions-in-wordpress-5-7/

WordPress 5.7 introduces a new helper function to more easily fetch the parent post's ID: get_parent_post()

This can also be used in conjunction with has_parent_post(), so you could have something like looks like:

<?php if ( has_parent_post() ) : ?>
    <a href="<?php the_permalink( get_parent_post() ); ?>">
        <?php
        echo sprintf(
            esc_html__( 'Back to parent page: %s', 'text-domain' ),
            get_the_title( get_parent_post() )
        );
        ?>
    </a>
<?php endif; ?>

Note that these functions accept a "child post ID" as a parameter, which defaults to the current post.

https://make.wordpress.org/core/2021/02/10/introducing-new-post-parent-related-functions-in-wordpress-5-7/

WordPress 5.7 introduces a new helper function to more easily fetch the parent post's ID: get_post_parent()

This can also be used in conjunction with has_post_parent(), so you could have something like looks like:

<?php if ( has_post_parent() ) : ?>
    <a href="<?php the_permalink( get_post_parent() ); ?>">
        <?php
        echo sprintf(
            esc_html__( 'Back to parent page: %s', 'text-domain' ),
            get_the_title( get_post_parent() )
        );
        ?>
    </a>
<?php endif; ?>

Note that these functions accept a "child post ID" as a parameter, which defaults to the current post.

https://make.wordpress.org/core/2021/02/10/introducing-new-post-parent-related-functions-in-wordpress-5-7/

Source Link

WordPress 5.7 introduces a new helper function to more easily fetch the parent post's ID: get_parent_post()

This can also be used in conjunction with has_parent_post(), so you could have something like looks like:

<?php if ( has_parent_post() ) : ?>
    <a href="<?php the_permalink( get_parent_post() ); ?>">
        <?php
        echo sprintf(
            esc_html__( 'Back to parent page: %s', 'text-domain' ),
            get_the_title( get_parent_post() )
        );
        ?>
    </a>
<?php endif; ?>

Note that these functions accept a "child post ID" as a parameter, which defaults to the current post.

https://make.wordpress.org/core/2021/02/10/introducing-new-post-parent-related-functions-in-wordpress-5-7/