wp_insert_attachment()通过代码为文章添加附件(图片/文件)

目录

描述

译文

该函数在媒体库中插入一个附件。函数与wp_update_attachment_metadata() 和wp_generate_attachment_metadata()联合使用。返回wp_posts表格所创建条目的编号。

原文

This function inserts an attachment into the media library. The function should be used in conjunction with wp_update_attachment_metadata() and wp_generate_attachment_metadata(). It returns the ID of the entry created in the wp_posts table.

This function is part of the low-level API used by WordPress for handling attachments. To perform the entire attachment upload and insertion process at once, you will want to use media_handle_upload() instead in most cases.

wp_insert_attachment() 描述

用法

 <?php wp_insert_attachment$attachment$filename$parent_post_id ); ?> 

wp_insert_attachment() 用法

参数

$attachment

(array) (必填) Array of data about the attachment that will be written into the wp_posts table of the database. Must contain at a minimum the keys post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type.

默认值: None

$filename

(string) (可选) Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be on the uploads directory. See wp_upload_dir()

默认值: false

$parent_post_id

(int) (可选) Attachments may be associated with a parent post or page. Specify the parent's post ID, or 0 if unattached.

默认值: 0

wp_insert_attachment() 参数

返回值

Returns the resulting post ID (int) on success or 0 (int) on failure.

wp_insert_attachment() 返回值

源文件

wp_insert_attachment() 函数的代码位于 wp-includes/post.php.

wp_insert_attachment() 源文件

相关