get_enclosed()
云策文档标注
概述
get_enclosed() 函数用于检索指定文章已包含的附件列表。它通过获取文章的自定义字段,筛选出 'enclosure' 键值,并返回处理后的附件数组。
关键要点
- 函数接受一个必需的整数参数 $post_id,表示文章 ID。
- 返回一个字符串数组,包含给定文章的附件列表。
- 内部使用 get_post_custom() 获取文章自定义字段,并遍历筛选 'enclosure' 键。
- 通过 apply_filters() 应用 'get_enclosed' 钩子,允许开发者过滤返回的附件列表。
代码示例
function get_enclosed( $post_id ) {
$custom_fields = get_post_custom( $post_id );
$pung = array();
if ( ! is_array( $custom_fields ) ) {
return $pung;
}
foreach ( $custom_fields as $key => $val ) {
if ( 'enclosure' !== $key || ! is_array( $val ) ) {
continue;
}
foreach ( $val as $enc ) {
$enclosure = explode( "n", $enc );
$pung[] = trim( $enclosure[0] );
}
}
/**
* Filters the list of enclosures already enclosed for the given post.
*
* @since 2.0.0
*
* @param string[] $pung Array of enclosures for the given post.
* @param int $post_id Post ID.
*/
return apply_filters( 'get_enclosed', $pung, $post_id );
}注意事项
- 函数从 WordPress 1.5.0 版本开始引入。
- 相关函数包括 get_post_custom() 和 apply_filters(),用于获取文章元数据和应用过滤器。
- 被 do_enclose() 函数使用,用于检查内容中的视频和音频链接并添加为附件。
原文内容
Retrieves enclosures already enclosed for a post.
Parameters
$post_idintrequired-
Post ID.
Source
function get_enclosed( $post_id ) {
$custom_fields = get_post_custom( $post_id );
$pung = array();
if ( ! is_array( $custom_fields ) ) {
return $pung;
}
foreach ( $custom_fields as $key => $val ) {
if ( 'enclosure' !== $key || ! is_array( $val ) ) {
continue;
}
foreach ( $val as $enc ) {
$enclosure = explode( "n", $enc );
$pung[] = trim( $enclosure[0] );
}
}
/**
* Filters the list of enclosures already enclosed for the given post.
*
* @since 2.0.0
*
* @param string[] $pung Array of enclosures for the given post.
* @param int $post_id Post ID.
*/
return apply_filters( 'get_enclosed', $pung, $post_id );
}
Hooks
- apply_filters( ‘get_enclosed’, string[] $pung, int $post_id )
-
Filters the list of enclosures already enclosed for the given post.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |