2

我有以下代码:

$wp_filetype = wp_check_filetype( basename( $image_url ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
    'guid' => $wp_upload_dir['url']. '/' . basename( $image_url ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($image_url)),
    'post_content' => '',
    'post_status' => 'publish'
);

$attach_id = wp_insert_attachment( $attachment, $image_url );

一切正常,但附加的 img 标签的 url 与 src 属性中的预期不同。( wp-contents/uploads 而不是 wp-contents/uploads/2013/03/ )

4

1 回答 1

1

尝试更改$wp_upload_dir['url']$wp_upload_dir['path']$uploads['baseurl'] . $uploads['subdir']

于 2013-03-03T10:15:59.683 回答