将自定义字段图像转换为WordPress中的特色图像?

时间:2010-12-06 23:13:48

标签: image wordpress

我运行了一个CSS库,我需要将自定义字段中的图库中的所有图像转换为特色图像。
有没有人有任何建议我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

好的,首先,如果你想完全避开TimThumb并且你的图片目前尺寸不合适,你需要find a plugin that can batch resize your media library

完成后,我们可以尝试自动执行查找每个特色图像附件的过程(即wp_posts表中的帖子条目),并设置后缩略图。

$uploads = wp_upload_dir();

// Get all attachment IDs and filenames
$results = $wpdb->get_results("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file'");

// Create an 'index' of attachment IDs and their filenames
$attachments = array();
foreach ($results as $row)
    $attachments[ intval($row->post_id) ] = $row->meta_value;

// Get all featured images
$images = $wpdb->get_results("SELECT post_id, meta_value AS 'url' FROM $wpdb->postmeta WHERE meta_key = 'Featured Image'");

// Loop over each image and try and find attachment post
foreach ($images as $image) {
    if (preg_match('#^https?://#', $image->url))
        $image->url = str_replace($uploads['baseurl'], '', $image->url); // get relative URL if absolute

    $filename = ltrim($image->url, '/');

    if ($attachment_ID = array_search($filename, $attachments)) {
        // found attachment, set post thumbnail and delete featured image
        update_post_meta($image->post_id, '_thumbnail_id', $attachment_ID);
        delete_post_meta($image->post_ID, 'Featured Image');
    }
}

声明

无法保证上述代码能够正常运行,而且完全基于我迄今为止根据您的意见评估的信息 - 这更像是一个建议,而不是一站式解决方案。