使用add_image_size() 定义自定义图像大小
1.注册自定义图像大小(functions.php)
if ( function_exists( \'add_image_size\' ) ) {
add_image_size( \'custom-image\', 440, 265, true ); //(hard cropped)
}
2。以自定义图像大小获取图像,但帖子的特色图像除外(functions.php)
function ravs_get_custom_image( $featured_img ){
global $post, $posts;
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => 1,
\'post_status\' => null,
\'post_parent\' => $post->ID,
\'exclude\' => $featured_img
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$img = wp_get_attachment_image( $attachment->ID, \'custom-image\' );
return $img;
}
}else{
echo \'Please attach images to your post\';
}
}
3。您的代码可能如下所示
(Note:为了获得帖子附件,我们需要排除特色图片,这样它就不会出现两次)
//using in loop
<div class="post-image">
<?php echo ravs_get_custom_image( get_post_thumbnail_id () ); ?>
</div><!-- end blog-image -->
4。输出可能如下所示
<div class="post-image">
<img width="440" height="265" src="http://localhost/wooplay.com/wp-content/uploads/2010/01/homepage-image-440x265.jpg" class="attachment-custom-image" alt="homepage-image">
</div><!-- end blog-image -->
重要链接:
get_posts()
get_post_thumbnaiul_id()
- wp_get_attachment_image()