我正在以编程方式将图片上传到我的 Wordpress 媒体库,并将它们设置为我的帖子的特色图片,效果很好。
我想生成这些特色图像的缩略图,就像 Wordpress 在通过“插入媒体”窗格添加图像时处理创建调整大小的缩略图的方式相同,在其中创建多个文件,每个文件都具有在文件名中指定的新尺寸。
不幸的是,我很难找到任何解释如何正确执行此操作的内容。
下面是相关代码。它成功地创建了帖子并将特色图片设置为媒体文件,但不会生成与之配套的缩略图。
任何帮助将不胜感激!
// If the post was created
if($post_id) {
// Add meta/custom field data
add_post_meta($post_id, 'gif_random_id', $randomId);
// Add uploaded file to the actual Wordpress image library
global $uploadURL;
$filename = $uploadURL . $randomId . '.' . $fileExtension;
$wp_filetype = wp_check_filetype(basename($filename), null);
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'publish'
);
$attach_id = …Run Code Online (Sandbox Code Playgroud) 我使用 wp_update_post 以编程方式从前端向帖子添加标题和标签。在此过程中,我遇到了自定义字段的令人头疼的问题:最初创建帖子时创建和填充的两个自定义字段之一的值已被删除,而另一个则完全正常。
这是我首先用来创建帖子的代码部分:
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'comment_status'=> 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_status' => 'publish',
'post_type' => 'custom'
)
);
// If the post was created properly
if($post_id) {
// Add meta/custom field data to post
add_post_meta($post_id, 'custom_random_id', $randomId);
add_post_meta($post_id, 'viewcount', '1');
Run Code Online (Sandbox Code Playgroud)
然后,这是我用来更新标题和标签的代码:
// Continue if untampered
if($new_hashed_value == $_POST['hash']) {
$updatePost = array();
$updatePost['ID'] = $post_id;
$updatePost['post_title'] = …Run Code Online (Sandbox Code Playgroud)