mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-06 21:54:28 +00:00
Posts, Post Types: Introduce new functions for determining if a post has a parent (has_post_parent()) and to fetch the post parent (get_post_parent()).
These functions are simple but reduce the logic needed in themes and plugins. Props ramiy, sebastian.pisula, birgire, audrasjb, xkon Fixes #33045 git-svn-id: https://develop.svn.wordpress.org/trunk@50127 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1963,3 +1963,28 @@ function wp_list_post_revisions( $post_id = 0, $type = 'all' ) {
|
||||
echo $rows;
|
||||
echo '</ul>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the parent post object for the given post.
|
||||
*
|
||||
* @since 5.7.0
|
||||
*
|
||||
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
|
||||
* @return WP_Post|null Parent post object, or null if there isn't one.
|
||||
*/
|
||||
function get_parent_post( $post = null ) {
|
||||
$wp_post = get_post( $post );
|
||||
return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given post has a parent post.
|
||||
*
|
||||
* @since 5.7.0
|
||||
*
|
||||
* @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post.
|
||||
* @return bool Whether the post has a parent post.
|
||||
*/
|
||||
function has_parent_post( $post = null ) {
|
||||
return (bool) get_parent_post( $post );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user