2014-10-08 71 views
0

我编码接受一个媒体ID作为属性的短代码。然后,此简码应以用add_image_size注册的现有尺寸显示图像。 问题:我的图像未以正确的尺寸显示。WordPress的主题显示调整大小的图像与PHP

说明:

  • 我的媒体形象已经在WP库被上传。原始文件大小很大(1227x924)。媒体有294

  • 我注册过的functions.php图像尺寸的ID:

    function my_after_setup_theme(){ 
        add_image_size('my-image-size', 210, 136, true); 
    } 
    add_action('after_setup_theme', 'my_after_setup_theme'); 
    
  • 我插入我的简码在我的网页之一:[my_shortcode imageid="294"]

  • 我的短码的代码:

    function my_shortcode_func($atts) 
    { 
        $a = shortcode_atts(array(
         'imageid' => 0, 
        ), $atts); 
        if ($a['imageid'] == 0) { 
         // default placeholder image 
         $img = 'img src="http://placehold.it/210x136"/>'; 
        } else { 
         // get resized (NOT WORKING !) 
         $img = wp_get_attachment_image($a['imageid'], 'my-image-size'); 
        } 
        return $img; 
    } 
    add_shortcode('my_shortcode', 'my_shortcode_func'); 
    
  • 我希望原始图像以正确的大小来调整(即:210x136)的新的缩略图文件。相反,$img显示原始图像(1227x924),在通过宽度和高度中等尺寸显示的HTML标签<img>属性:

我在做什么错?

感谢您的帮助。

+0

最新的输出是什么返回$ img? – Bradley 2014-10-08 13:40:59

回答

1

上传媒体后是否注册了缩略图大小?如果是这样,您将需要使用插件重新生成缩略图。

此外,我不相信你需要像你所做的那样缩小缩略图大小声明,我有以下几点:

if (function_exists('add_theme_support')) 
{ 
    add_theme_support('post-thumbnails'); // Featured image support for posts - you may not need this bit. 
    add_image_size('large', 700, '', true); // Large Thumbnail - This is the bit you would need 
} 

因此,这将是值得一试,如果实际生成缩图与开始,因为我从来没有做过你在你的问题中定义的方式。

+0

你是对的!我使用插件重新生成缩略图,一切正常......谢谢!虽然我觉得这种行为有点奇怪......如果函数wp_get_attachment_image不存在,函数会立即重新生成图像。 – 2014-10-08 17:32:59