函数文档

has_post_parent()

💡 云策文档标注

概述

has_post_parent() 函数用于检查指定文章是否有父文章。它基于 get_post_parent() 函数实现,返回布尔值。

关键要点

  • 函数接受一个可选参数 $post,可以是文章 ID、WP_Post 对象或 null(默认使用全局 $post)。
  • 返回值为布尔类型,表示文章是否有父文章。
  • 该函数在 WordPress 5.7.0 版本中引入。

代码示例

function has_post_parent( $post = null ) {
    return (bool) get_post_parent( $post );
}

注意事项

相关函数 get_post_parent() 可用于获取父文章对象,位于 wp-includes/post-template.php 文件中。


📄 原文内容

Returns whether the given post has a parent post.

Parameters

$postint|WP_Post|nulloptional
Post ID or WP_Post object. Default is global $post.

Default:null

Return

bool Whether the post has a parent post.

Source

function has_post_parent( $post = null ) {
	return (bool) get_post_parent( $post );
}

Changelog

Version Description
5.7.0 Introduced.