2013-06-20 52 views
0

我正在开发一个CMS网站的WordPress。 我的网站模板有多个页面,在这些页面中,我有不同大小的图像。图像将成为这些帖子的特色图片。WordPress的缩略图上传多个自定义尺寸

因此,让我们说在主页,我有一个精选的股利,其中图像大小应该是720×963。

在同一个页面中,该特征DIV下面,我有其他职位一个div当图像尺寸为350×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将图像大小调整为我需要的确切大小。

回答

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'); 
+0

这是剪裁,但不严格。我的意思是720高度正在裁剪,但它自动裁剪宽度。 – prakashchhetri

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

+0

如果我想同时创建150 * 150和300 * 300的图像,是否有任何方法可以创建多个尺寸的图像,是否可以完成? –