Wordpress Thumbnail上传多个自定义大小

时间:2013-06-20 12:22:26

标签: php wordpress resize

我正在使用wordpress开发CMS网站。 我的网站模板有多个页面,在这些页面中,我有不同大小的图像。图像将是这些帖子的特色图片。

让我们在主页上说,我有一个特色div,图像大小应该是720 X 963.

在同一页面中,在特色div下方,我有一个div用于其他帖子,图片大小为350 X 224。

最后,我有一个页面,我在一个特定类别中显示帖子,如图库的形式,其中图像缩略图的大小为257 X 161。

在所有这些页面中提取的图像是帖子的特色图像。

我尝试将functions.php修改为

// Set up custom post image sizes
if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'home-post-thumbnail', 350, 224 );
}

// Set up custom post image sizes
if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'home_featured-post-thumbnail', 720, 963 );
}

并在主题中将其用作

<?php the_post_thumbnail( $size = 'home_featured-post-thumbnail') ?>但它不起作用。我浏览了wp-content文件夹,看看调整后的文件是否可用,但事实并非如此。

如何强制wordpress将图像大小调整为我需要的大小。

2 个答案:

答案 0 :(得分:1)

您应该首先检查add_theme_support功能,然后再查看add_image_size

if(function_exists('add_theme_support'))
add_theme_support('post-thumbnails');
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'home-post-thumbnail', 350, 224 ,true);
}

// Set up custom post image sizes
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'home_featured-post-thumbnail', 720, 963,true );
}
 the_post_thumbnail('home-post-thumbnail');// for automatically crop when uploaded

并检索缩略图

get_the_post_thumbnail($post->ID, 'home_featured-post-thumbnail');  

答案 1 :(得分:0)

试试这个:

$image = wp_get_image_editor( 'cool_image.jpg' ); // Return an implementation that extends <tt>WP_Image_Editor</tt>

if ( ! is_wp_error( $image ) ) {
    $image->rotate( 90 );
    $image->resize( 300, 300, true );
    $image->save( 'new_image.jpg' );
}

您可能也会对此感兴趣:https://codex.wordpress.org/Class_Reference/WP_Image_Editor

相关问题