pings_open()
云策文档标注
概述
pings_open() 函数用于判断当前文章是否允许接收 pingback 和 trackback。它接受一个可选参数来指定文章,并返回布尔值表示 ping 状态是否为“open”。
关键要点
- 函数检查文章的 ping_status 属性是否为“open”,以确定是否接受 pings。
- 接受可选参数 $post,可以是文章 ID 或 WP_Post 对象,默认为当前文章。
- 返回布尔值:true 表示 pings 被接受,false 表示不接受。
- 通过 apply_filters('pings_open', $pings_open, $post_id) 提供过滤器钩子,允许开发者自定义逻辑。
代码示例
function pings_open( $post = null ) {
$_post = get_post( $post );
$post_id = $_post ? $_post->ID : 0;
$pings_open = ( $_post && ( 'open' === $_post->ping_status ) );
/**
* Filters whether the current post is open for pings.
*
* @since 2.5.0
*
* @param bool $pings_open Whether the current post is open for pings.
* @param int $post_id The post ID.
*/
return apply_filters( 'pings_open', $pings_open, $post_id );
}注意事项
- 函数自 WordPress 1.5.0 版本引入,是主题开发中常用的条件标签之一。
- 相关函数包括 get_post() 用于获取文章数据,以及 apply_filters() 用于应用过滤器。
- 在主题或插件开发中,可使用此函数控制 pingback 和 trackback 的显示或处理逻辑。
原文内容
Determines whether the current post is open for pings.
Description
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters
Source
function pings_open( $post = null ) {
$_post = get_post( $post );
$post_id = $_post ? $_post->ID : 0;
$pings_open = ( $_post && ( 'open' === $_post->ping_status ) );
/**
* Filters whether the current post is open for pings.
*
* @since 2.5.0
*
* @param bool $pings_open Whether the current post is open for pings.
* @param int $post_id The post ID.
*/
return apply_filters( 'pings_open', $pings_open, $post_id );
}
Hooks
- apply_filters( ‘pings_open’, bool $pings_open, int $post_id )
-
Filters whether the current post is open for pings.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |