wp_insert_attachment

函式
wp_insert_attachment ( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true )
引數
  • (string|array) $args Arguments for inserting an attachment.
    Required:
  • (string|false) $file Optional. Filename.
    Required:
    Default: false
  • (int) $parent Optional. Parent post ID.
    Required:
  • (bool) $wp_error Optional. Whether to return a WP_Error on failure. Default false.
    Required:
    Default: false
  • (bool) $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
    Required:
    Default: true
返回值
  • (int|WP_Error) The attachment ID on success. The value 0 or WP_Error on failure.
相關
  • wp_insert_post()
定義位置
相關方法
wp_insert_commentwp_delete_attachmentwp_count_attachmentsis_attachmentwp_get_attachment_url
引入
2.0.0
棄用
-

wp_insert_attachment: 這個函式用來在媒體庫中插入一個新的附件。它接受一個附件資料的陣列作為引數,並返回新附件的ID。

插入一個附件。

如果你在$args引數中設定了’ID’,這將意味著你正在更新並試圖更新附件。你也可以通過設定’post_name’或’post_title’鍵來設定附件的名稱或標題。

你可以通過設定’post_date’和’post_date_gmt’鍵值來手動設定附件的日期。

預設情況下,評論將使用是否允許評論的預設設定。你可以通過設定’comment_status’鍵的值來手動關閉它們或保持它們開放。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) {
$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, $wp_error, $fire_after_hooks );
}
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) { $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, $wp_error, $fire_after_hooks ); }
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) {
	$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, $wp_error, $fire_after_hooks );
}

常見問題

FAQs
檢視更多 >