gpt4 book ai didi

php - 尝试以编程方式生成 WordPress 缩略图

转载 作者:搜寻专家 更新时间:2023-10-31 20:50:06 27 4
gpt4 key购买 nike

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

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

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

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

不管怎么说,这是行不通的。通过 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_generate_attachment_metadata 之后的 wp_update_attachment_metadata

尝试添加

wp_update_attachment_metadata($attach_id, $attach_data);

之后

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

所以 if block 看起来像

 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com