WordPress:Function Reference/wp insert attachment

来自站长百科
Xxf3325讨论 | 贡献2008年7月19日 (六) 11:24的版本 (新页面: ==Description== This function inserts an attachment into the media library. The function should be used in conjunction with [[WordPress:Function_Reference/wp_update_attachment_metadata|wp...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

Description

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(). Returns the ID of the entry created in the wp_posts table.

Usage

%%% <?php wp_insert_attachment( $attachment, $filename, $parent_post_id ); ?> %%%

Example

To insert an attachment to a parent with a post ID of 37:

<?php
  $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  wp_update_attachment_metadata( $attach_id,  $attach_data );
?>

Parameters

Related

wp_get_attachment_url(), wp_delete_attachment(), wp_insert_post()