我正在尝试以编程方式添加一堆帖子,然后添加它们的附带图像。我有一个包含所有图像文件名的数组,我已经能够将它们添加到数据库中,但我似乎无法创建正确的缩略图。

这些帖子来自位于 wp-content/uploads/dirname/的 csv。他们的文件名中有与 CSV 中的 ID 相对应的数字,这就是我知道需要将哪些图像添加到哪个帖子 ID 的方式。

我已经让 wp_insert_attachment() 部分在他们自己的小目录中处理图像,但我无法生成缩略图。我安装了重新生成缩略图插件,它能够生成它们,但我似乎无法以编程方式让它发生。

我认为这可能是因为 wp_generate_attachment 需要照片位于/uploads/2011/12/(例如),所以我开始移动图像然后尝试添加它们。无论如何,这是有道理的,因为我有点想制作副本,而不是将 5 或 6 种不同的媒体尺寸添加到我的 wp-content/uploads/dirname/dir。

无论如何,它不起作用。通过 PHP 的副本移动图像不起作用,并且不会生成缩略图。

foreach ($frazerfiles[$stock_num] as $stock_img){
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  echo "... ...Trying to add attachment metadata...";

  // copy the file to the upload dir
  $uploads = wp_upload_dir();
  $file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
  if (copy($file_to_move, $uploads['path']."/")) {
    echo "Moved $file_to_move to ".$uploads['path']."/";
    $my_moved_file = $uploads['path']."/".$stock_img;

    // I think this is failing because the images aren't in the upload dir.
    $attachment = array(
       'post_mime_type' => $wp_filetype['type'],
       'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
       'post_content' => '',
       'post_status' => 'inherit'
    );
    if ($attach_id = wp_insert_attachment( $attachment, $my_moved_file, $newpostid )) {
      if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
        echo "...success for $my_moved_file: ID:$attach_id<br />\n";
      } else {
        echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
        print_r($attach_data);
      }
    } else { // inserting attachment failed
      echo "Insert attachment failed for $my_moved_file to $newpostid<br />\n";
    }
  } else {
    echo "Failed moving $file_to_move to ".$uploads['path']."/";
  }


}// images foreach

最佳答案

在与 WordPress 核心文件进行交叉检查后,似乎唯一遗漏的是 wp_update_attachment_metadata 紧跟在 wp_generate_attachment_metadata 之后。

尝试添加

wp_update_attachment_metadata($attach_id, $attach_data);


echo "...success for $my_moved_file: ID:$attach_id<br />\n";

所以 if 块看起来像
 if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
    echo "...success for $my_moved_file: ID:$attach_id<br />\n";
    wp_update_attachment_metadata($attach_id, $attach_data);
  } else {
    echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
    print_r($attach_data);
  }

建议:(与问题无关)

移动线
require_once(ABSPATH . 'wp-admin/includes/image.php');

在 for 循环之外(上方)。

更新1:
添加修复复制问题的建议。
您错过了“检查文件类型”这一行。
(如果目标文件( $my_moved_file )已经存在,PHP 复制功能将失败)

更改此代码
// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
if (copy($file_to_move, $uploads['path']."/")) {
echo "Moved $file_to_move to ".$uploads['path']."/";
$my_moved_file = $uploads['path']."/".$stock_img;

// I think this is failing because the images aren't in the upload dir.
$attachment = array(
   'post_mime_type' => $wp_filetype['type'],
   'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
   'post_content' => '',
   'post_status' => 'inherit'
);


// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
$my_moved_file = $uploads['path']."/".$stock_img;
if (copy($file_to_move, $my_moved_file)) {
    echo "Moved $file_to_move to ".$my_moved_file;

    // Check the file type
    $wp_filetype = wp_check_filetype(basename($my_moved_file), null );

    $attachment = array(
       'post_mime_type' => $wp_filetype['type'],
       'post_title' => preg_replace('/\.[^.]+$/', '', basename($my_moved_file)),
       'post_content' => '',
       'post_status' => 'inherit'
    );

关于php - 尝试以编程方式生成 WordPress 缩略图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8610747/

10-10 01:19
查看更多