wordpress wp_insert_attachment()url文件目录错误

时间:2013-03-03 10:01:53

标签: wordpress upload

我有以下代码:

$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标记在其src属性中具有与预期不同的url。 (wp-contents / uploads而不是wp-contents / uploads / 2013/03 /)

1 个答案:

答案 0 :(得分:1)

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