wp_insert_attachment()
wp_insert_attachment( string|array $args, string $file …
wp_insert_attachment( string|array $args, string $file = false, int $parent, bool $wp_error = false )
插入附件。
Insert an attachment.
目录锚点:#说明#参数#源码#笔记
说明(Description)
如果在$args参数中设置了’ID’,则表示您正在更新并尝试更新附件。您也可以通过设置键“post_name”或“post_title”来设置附件名称或标题。您可以通过设置“post_date”和“post_date_gmt”键值手动设置附件的日期。默认情况下,注释将使用是否允许注释的默认设置。您可以手动关闭它们,也可以通过设置“注释状态”键的值来保持打开状态。另请参阅wp_insert_post()
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $args | (string | array) | 用于插入附件的参数。 |
| $file | (string) | 文件名。 |
| $parent | (int) | 父帖子ID。 |
| $wp_error | (bool) | 失败时是否返回WP_错误。 |
源码(Source)
/**
* Insert an attachment.
*
* If you set the 'ID' in the $args parameter, it will mean that you are
* updating and attempt to update the attachment. You can also set the
* attachment name or title by setting the key 'post_name' or 'post_title'.
*
* You can set the dates for the attachment manually by setting the 'post_date'
* and 'post_date_gmt' keys' values.
*
* By default, the comments will use the default settings for whether the
* comments are allowed. You can close them manually or keep them open by
* setting the value for the 'comment_status' key.
*
* @since 2.0.0
*
* @see wp_insert_post()
*
* @param string|array $args Arguments for inserting an attachment.
* @param string $file Optional. Filename.
* @param int $parent Optional. Parent post ID.
* @return int Attachment ID.
*/
function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
$defaults = array(
'file' => $file,
'post_parent' => 0
);
$data = wp_parse_args( $args, $defaults );
if ( ! empty( $parent ) ) {
$data['post_parent'] = $parent;
}
$data['post_type'] = 'attachment';
return wp_insert_post( $data );
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 4.7.0 | wp-includes/post.php | 15 | 17 |
笔记(Notes)
例子
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。




还没有任何评论,赶紧来占个楼吧!