我正在wordpress中显示第一个img帖子,如缩略图!但是我正在尝试将默认大小的拇指应用于此第一张图片,但没有成功:
我的默认尺寸是:
add_image_size('video-large', 565, 318, true);
add_image_size('video-single', 750, 330, true);
add_image_size('blog-single', 728, 288, true);
add_image_size('video-medium', 370, 208, true);
add_image_size('video-small', 175, 98, true);
add_image_size('widget-post', 55, 55, true);
add_image_size('term-listing', 170, 125, true);
我想将“视频大”尺寸应用于get_first_image,因为此过滤器裁剪了图像,并且我想要它。
我尝试:
<img src="<?php echo get_first_image('video-large');?>
但是,不起作用!
// Get first image
function get_first_image() {
global $post, $posts;
$first_image = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_image = $matches[1][0];
if ( empty($first_image) ) {
// Defines a default image
// Set the default image if there are no image available inside post content
$first_image = "/img/default.jpg";
}
return $first_image;
}
答案 0 :(得分:0)
如果我正确理解了您的问题,我认为您可以正确使用以下功能。
/*Find the image id from a URL*/
function url_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
确定帖子中是否有特色图片,如果没有,请在帖子内容中查找第一张图片,$ size传递缩略图大小,$ url确定是返回URL还是完整图片标签
function checkImageType($size, $type) {
global $post;
$content = $post->post_content;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
$first_img = $matches[1][0];
/*If there's a featured image, show it*/
if (get_the_post_thumbnail($post_id) != '' ) {
if($type=='url') {
the_post_thumbnail_url($size);
} else {
the_post_thumbnail($size);
}
} else {
/*No featured image, so we get the first image inside the post content*/
if ($first_img) {
//let's get the correct image dimensions
$image_id = url_get_image_id($first_img);
$image_thumb = wp_get_attachment_image_src($image_id, $size);
// if we've found an image ID, correctly display it
if($image_thumb) {
if($type=='url') {
echo $image_thumb[0];
} else {
echo '<img src="'.$image_thumb[0].'" alt="'.get_the_title().'"/>';
}
} else {
//if no image (i.e. from an external source), echo the original URL
if($type=='url') {
echo $first_img;
} else {
echo '<img src="'.$first_img.'" alt="'.get_the_title().'"/>';
}
}
}
}
}
一些示例使用:
checkImageType('full', 'url');
// Returns: http://your-domain/image-url.jpg)
checkImageType('post-thumb');
// Returns: <img src="http://your-domain/image-url.jpg" alt="Alt text">
答案 1 :(得分:0)
在function.php中添加WordPress自定义图像大小
// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );
// Add featured image sizes
add_image_size( 'featured-large', 640, 294, true ); // width, height, crop
add_image_size( 'featured-small', 320, 147, true );
// Add other useful image sizes for use through Add Media modal
add_image_size( 'medium-width', 480 );
add_image_size( 'medium-height', 9999, 480 );
add_image_size( 'medium-something', 480, 480 );
// Register the three useful image sizes for use in Add Media modal
add_filter( 'image_size_names_choose', 'wpshout_custom_sizes' );
function wpshout_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'medium-width' => __( 'Medium Width' ),
'medium-height' => __( 'Medium Height' ),
'medium-something' => __( 'Medium Something' ),
) );
}