为什么当我尝试与帖子一起插入图像附件时,wp_get_attachment_url会给我一个非常错误的文件路径?

时间:2012-03-22 作者:crowjonah

我正在WPMS上使用一个新的博客(3.0.1,不可更新)来聚合来自同一安装上几个博客的帖子(及其特色图片),由于防火墙的限制,我必须通过编程而不是通过预先制作的插件来完成。我试过几种方法this method 是最有希望的。以下是我做的:

switch_to_blog(oldblognumber);
$zargs  = array( \'numberposts\' => 1, \'category_name\' => \'featured\');
$zlastpost = get_posts( $zargs );
foreach($zlastpost as $post) : setup_postdata($post);
    $extrapost = array();
    $extrapost[\'post_title\'] = get_the_title();
    $extrapost[\'post_content\'] = get_the_content();
    $extrapost[\'comment_status\'] = \'closed\';
    $extrapost[\'post_status\'] = \'publish\';
    $extrapost[\'post_date\'] = get_the_date( $d = \'Y-m-d H:i:s\');
    $extrapost[\'post_category\'] = array(80);
    $upload_dir = wp_upload_dir();
    $oldid = get_the_ID();
    $image_url = wp_get_attachment_url(get_post_thumbnail_id($oldid));
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);
    switch_to_blog(newblognumber);          
        $file = $upload_dir[\'path\'] . \'/\' . $filename; //removed the conditional cuz it was giving me trouble
        file_put_contents($file, $image_data);
        $post_id = wp_insert_post($extrapost);
        $wp_filetype = wp_check_filetype($filename, null );
        $attachment = array(
            \'post_mime_type\' => $wp_filetype[\'type\'],
            \'post_title\' => sanitize_file_name($filename),
            \'post_content\' => \'\',
            \'post_status\' => \'inherit\',
        );                  
        $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
        require_once(ABSPATH . \'wp-admin/includes/image.php\');
        $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
        wp_update_attachment_metadata( $attach_id, $attach_data );
        set_post_thumbnail( $post_id, $attach_id );                       
    restore_current_blog();
    wp_reset_postdata();
endforeach;
restore_current_blog();
插入后的效果很好,图像也很好,但在某些地方,事情变得混乱了,它吐出了一个荒谬的附件url:http://myblogs.com/newblog/wp-content/blogs.dir/newblognumber/files//internet/http/wp-content/blogs.dir/oldblognumber/files/year/month/filename.jpg.

同样,在我请求\\u post\\u缩略图的循环中,我得到了一个损坏的图像,其src为http://myblogs.com/newblog/files//internet/http/wp-content/blogs.dir/oldblognumber/files/year/month/filename-200x100.jpg

值得一提的是,由于我使用的是WP的pre-set\\u post\\u缩略图版本,所以我只需在我的newblog函数中手动定义该函数。php:

function set_post_thumbnail( $post, $thumbnail_id ) {
$post = get_post( $post );
$thumbnail_id = absint( $thumbnail_id );
if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
   $thumbnail_html = wp_get_attachment_image( $thumbnail_id, \'thumbnail\' );
   if ( ! empty( $thumbnail_html ) ) {
       update_post_meta( $post->ID, \'_thumbnail_id\', $thumbnail_id );
       return true;
   }
}
return false;
}
我尝试过伪造guid和使用正则表达式来强制url提交,但都没有用。如果你对我有任何指导,请告诉我!

我还尝试了另一种方法来设置插入帖子的帖子缩略图,但这只是调用oldblog的上载目录中的原始文件,这是不够的,因为我想为新博客的媒体库生成新的图像大小,我不想遍历并将其添加到每个原始oldblog中。这是一次尝试:

switch_to_blog(oldblognumber);
$zargs  = array( \'numberposts\' => 1, \'category_name\' => \'featured\');
$zlastpost = get_posts( $zargs );
foreach($zlastpost as $post) : setup_postdata($post);
    $extrapost = array();
    $extrapost[\'post_title\'] = get_the_title();
    $extrapost[\'post_content\'] = get_the_content();
    $extrapost[\'comment_status\'] = \'closed\';
    $extrapost[\'post_status\'] = \'publish\';
    $extrapost[\'post_date\'] = get_the_date( $d = \'Y-m-d H:i:s\');
    $extrapost[\'post_category\'] = array(80);
    $oldid = get_the_ID();
    $thumbo = get_the_post_thumbnail($oldid);
    $filename = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    switch_to_blog(68);
        $post_id = wp_insert_post($extrapost);
        $wp_filetype = wp_check_filetype(basename($filename), null );
            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                \'guid\' => _wp_relative_upload_path( $filename ), 
                \'post_mime_type\' => $wp_filetype[\'type\'],
                \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($filename)),
                \'post_content\' => \'\',
                \'post_status\' => \'inherit\'
            );
            $attach_id = wp_insert_attachment( $attachment, false, $post_id );
            require_once(ABSPATH . \'wp-admin/includes/image.php\');
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            update_post_meta( $post->ID, \'_thumbnail_id\', $attachment_id );
            wp_update_attachment_metadata( $attach_id, $attach_data );
            set_post_thumbnail( $post_id, $attach_id);        
        restore_current_blog();
        wp_reset_postdata();
    endforeach;
restore_current_blog();     
我尝试过向wp\\u insert\\u attachment的$filename参数提供许多内容,但这似乎不是我的答案。

1 个回复
最合适的回答,由SO网友:crowjonah 整理而成

原来我用wp\\u insert\\u attachment查错了树(或者至少有点不同)。media\\u sideload\\u image成功地从同一多站点安装上的其他博客中提取附件,将其复制到聚合博客的上载目录,并生成缩略图,而wp\\u insert\\u attachment正在做它应该做的事情,这恰好不是我想要的。我意识到这只是对其他类似功能的使用的一个很好的解释,但我想我会在这里发布我的解决方案,以防有人碰巧更成功地应用这种特定的解决方案组合。

我仍然感觉到我并没有以最有效的方式来做这件事,但它现在起作用了:

function switch_and_insert($srcblog, $targetcat, $fromcat) {
  switch_to_blog($srcblog);
  $args  = array( \'numberposts\' => 1, \'category_name\' => $fromcat);
  $flastpost = get_posts( $args );
  foreach($flastpost as $post) : setup_postdata($post);
    $extrapost = array();
    $extrapost[\'post_title\'] = get_the_title($post);
    $extrapost[\'post_content\'] = get_the_post_thumbnail();
    $extrapost[\'comment_status\'] = \'closed\';
    $extrapost[\'post_status\'] = \'publish\';
    $extrapost[\'post_date\'] = get_the_date( $d = \'Y-m-d H:i:s\');
    $extrapost[\'post_category\'] = array($targetcat);
    $oldid = get_the_ID();
    if ( has_post_thumbnail($oldid)) {
        $filename = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    }
    else $filename = $forchomper; // URL of backup image
    switch_to_blog($aggregator_blog_id);
        $post_id = wp_insert_post($extrapost);
        $wp_filetype = wp_check_filetype(basename($filename), null );
        $wp_upload_dir = wp_upload_dir();
        $attachment = array(
            \'guid\' => _wp_relative_upload_path( $filename ), 
            \'post_mime_type\' => $wp_filetype[\'type\'],
            \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($filename)),
            \'post_content\' => \'\',
            \'post_status\' => \'inherit\'
        );
        require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
        require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
        require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
        $sideloaded = media_sideload_image($filename, $post_id);
        $attachments = get_children($post_id);
        foreach ( $attachments as $attachment_id => $attachment );
        set_post_thumbnail($post_id, $attachment_id);
    restore_current_blog();
    wp_reset_postdata();
  endforeach;
  restore_current_blog();
}
这可能不值得注意,但我要提到的是,使用此方法提供了一个非常方便的机会,可以在原始帖子没有占位符图像的情况下,将占位符图像应用为帖子缩略图/特色图像,这与更广泛建议的依赖于用于显示占位符的主题内的条件的解决方案不同,实际上并没有将其指定为帖子的缩略图。我意识到大多数时候这是更好的解决方案,但在我的情况下,这些帖子必须有自己附加的特色图片,即使它们是重复的。

以下是一些有帮助的事情:https://wordpress.stackexchange.com/a/19847/14351http://old.nabble.com/Uploading-image-attachments-from-the-front-end-td26307647.html

结束

相关推荐

merging two multisites

我需要合并两个现有的多站点。每个都有大约40个博客,最终结果应该是所有80个博客都位于一个WP安装上。关于如何最好地完成这项任务,其他人手动完成这项任务,在终端服务器上创建每个新博客和用户,然后对每个新博客使用导出/导入,有什么建议或经验吗?我希望这是一种有效的方法,也可以应用于更大的多站点。直接数据库操作和脚本自动化。。如果有人有经验或能提出一个好的解决方案,我有兴趣支付帮助费用。