在single.php中显示特定的缩略图大小

时间:2015-03-21 08:41:49

标签: wordpress thumbnails

目前我正在构建一个Wordpress主题,我正在尝试显示一个特定的图像大小,该大小在我的single.php中被裁剪,因此在各个帖子页面上都没有运气。

这是我的functions.php

add_theme_support( 'post-thumbnails' );
add_image_size('new-thumbnail-size',600,340, true);

这是我的single.php

<?php if( get_the_post_thumbnail() ) : ?>
    <div class = 'postImgCon'>
        <div class="feat-img">
             <?php the_post_thumbnail('new-thumbnail-size'); ?>
        </div>
    </div> 
<?php endif; ?> 

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您正在使用正确的函数声明支持发布缩略图并添加图片大小,但您需要在after_setup_theme挂钩内执行此操作。

内部functions.php更改:

add_theme_support( 'post-thumbnails' );
add_image_size('new-thumbnail-size',600,340, true);

要:

function wpse_setup_theme() {
    add_theme_support( 'post-thumbnails' );

    add_image_size( 'new-thumbnail-size', 600, 340, true );
}
add_action( 'after_setup_theme', 'wpse_setup_theme' );

此外,您还使用了错误的功能来检查帖子是否包含缩略图。它应该仍然有用,但这是正确的方法。

内部single.php更改:

<?php if( get_the_post_thumbnail() ) : ?>

要:

<?php if ( has_post_thumbnail() ) : ?>

最后,您需要注意,您上传的所有图片都不会被裁剪为600 x 340,因此您无法获得您期望的输出。您需要重新生成现有图像。请查看以下插件:https://wordpress.org/plugins/regenerate-thumbnails/

答案 1 :(得分:-1)

试试这个 ....

 <div class="feat-img">
   <?php echo the_post_thumbnail($page->ID, "new-thumbnail-size'); ?>

...

相关问题