在我的wp_theme中我有这个功能来获取帖子的第一张图片作为缩略图
function get_first_image() {
global $post;
$first_img = '';
preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', do_shortcode( $post->post_content, 'gallery' ), $matches );
$first_img = isset( $matches[1][0] ) ? $matches[1][0] : null;
if ( empty( $first_img ) ) {
return get_template_directory_uri() . '/assets/images/empty.png'; // path to default image.
}
// Now we have the $first_img but we want the thumbnail of that image.
$explode = explode( '.', $first_img );
$count = count( $explode );
$size = '-150x150'; // Our panel ratio (2:1) 312x156 for lighther page, 624x312 for retina; use add_image_size() and Force Regenerate Thumbnails plugin when changing sizes.
$explode[ $count -2 ] = $explode[ $count -2 ] . '' . $size;
$thumb_img = implode( '.', $explode );
return $thumb_img;}
但是某些图片有自定义尺寸,此功能在这种情况下不起作用
像这样: https://yawar.ir/wp-content/uploads/2014/08/yawar-1024x682.jpg如何强制更改具有其他尺寸的图像的网址?
答案 0 :(得分:1)
关于缩略图的WP codex页面说明您可以指定自己的尺寸:https://codex.wordpress.org/Post_Thumbnails查看“添加新的缩略图后尺寸”。也许这会指出你正确的方向。
除此之外,如何不指定尺寸呢?那么也许浏览器会以自然尺寸显示图像?