Wordpress图像大小介质显示不同的大小

时间:2017-09-24 13:41:18

标签: php wordpress

感谢您在这里寻找并尝试提供一些有用的答案。最近我下载了插件https://wordpress.org/plugins/olevmedia-shortcodes/,它工作得很好。但是当我尝试使用“最近发布”的短代码时,我将面临与您在下面的图像上看到的图像大小相关的问题:

enter image description here

看起来很难看。我打开了一个代码,找到了这个函数的一小段代码:

$out .= '<div class="omsc-recent-posts-title"><h3><a href="'. get_permalink() .'">'. get_the_title() .'</a></h3></div>';

                if( $thumbnail && has_post_thumbnail() ) {

                    $img = wp_get_attachment_image_src( get_post_thumbnail_id(), apply_filters('omsc_sc_recent_posts_img_size', 'medium'));
                    if($img) {
                        $img_html=apply_filters('omsc_sc_recent_posts_img', '<a href="'. get_permalink() .'"><img src="'.$img[0].'" alt="'.esc_attr($post->post_title).'" /></a>', array(
                            'img_src' => $img[0],
                            'link' => get_permalink(),
                            'alt' => $post->post_title,
                        ));
                        $out.='<div class="omsc-recent-posts-thumb">'.$img_html.'</a></div>';
                    }
                }

然后我查看图像大小并说:

$img = wp_get_attachment_image_src( get_post_thumbnail_id(), apply_filters('omsc_sc_recent_posts_img_size', 'medium'));

通过FireBug查看对于某些帖子,“中”大小设置为222x221,其他为300x300,其余为222x167

我想做的就是让所有缩略图都具有相同的尺寸。所以,我为我的主题的function.php文件创建了这段代码:

if ( function_exists( 'add_image_size' ) ) { 
add_image_size( 'custom-recpost-thumb', 222, 221,true ); 
}

然后对于插件php文件,我以这种方式调用了该函数:

$img = wp_get_attachment_image_src( get_post_thumbnail_id(), apply_filters('omsc_sc_recent_posts_img_size', 'custom-recpost-thumb'));

我也看到了同样的情况。绝对没有变化。我做错了什么?

2 个答案:

答案 0 :(得分:1)

我可以看到的问题是你上传的图片尺寸不一样,所以它适合你的宽度,自动高度可以通过两种方式解决这个问题1.修复你的图片用css no.2上传或装上它=使你的盒子保持img你想要的宽度和高度并使其溢出隐藏然后拍摄你的图像并说它必须与盒子一样高,宽度必须是自动的原因,大多数图片是作为一个风景,所以这样你的图片可以适合..为记录它不是一个很好的方式去做,我建议你上传它们之前修复你的图片

答案 1 :(得分:0)

不同尺寸的原始图像会创建不同大小的&#34;中等&#34;图片。这是因为WordPress中的媒体设置可以容纳不同大小的图像。通常,这些像素设置为任何尺寸的原始图像提供相当好看的中等尺寸图像。我怀疑WordPress会覆盖你的函数调用以显示&#34;最好的&#34;它可以基于算法和图像优化的图像。

即使使用WordPress.org的编码选项,WP仍然会使用一些通用设置来创建&#34; good&#34;网站。也许它是一个声誉的东西,也许它是技术组织告诉用户最适合他们的另一个例子。

无论如何,我同意Anker的观点,即在上传之前优化图像是一种更好的解决方案。它将为您提供更多控制,并提高WP将根据您的需要显示图像的机会。

相关问题