使用URL调整WordPress中图像的大小

我需要调整一些帖子的图像大小。我可以获取存储在使用Types插件创建的postmeta中的图像URL。
因此, 使用postmeta可以获取URL, 但是如何调整特定帖子类型的图像大小?
#1首先, 你必须从图片网址中找到附加的图片ID。要从图像URL获取附加的图像ID, 请在主题functions.php文件中添加以下函数:

function pn_get_attachment_id_from_url( $attachment_url = '' ) { global $wpdb; $attachment_id = false; // If there is no url, return. if ('' == $attachment_url) return; // Get the upload directory paths $upload_dir_paths = wp_upload_dir(); // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image if (false !== strpos($attachment_url, $upload_dir_paths['baseurl'])) {// If this is the URL of an auto-generated thumbnail, get the URL of the original image $attachment_url = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url); // Remove the upload path base directory from the attachment URL $attachment_url = str_replace($upload_dir_paths['baseurl'] . '/', '', $attachment_url); // Finally, run a custom database query to get the attachment ID from the modified attachment URL $attachment_id = $wpdb-> get_var($wpdb-> prepare("SELECT wposts.ID FROM $wpdb-> posts wposts, $wpdb-> postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = 'http://www.srcmini.com/%s' AND wposts.post_type = 'attachment'", $attachment_url)); }return $attachment_id; }

有关更多信息, 请参见url-https://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/
然后我们需要在function.php中使用图像调整大小功能:
add_image_size( 'latestproperty_thumb', 370, 293, true );

要获取图像附件ID, 请使用:
$attachid = pn_get_attachment_id_from_url($url);

安装完成后, 请https://wordpress.org/plugins/regenerate-thumbnails/。然后转到” 工具” -> ” 重新生成缩略图” 并重新生成所有缩略图。
【使用URL调整WordPress中图像的大小】之后, 使用它来获取重新生成的图像URL:
$src = http://www.srcmini.com/wp_get_attachment_image_src($attachid,'latestproperty_thumb');

    推荐阅读