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

Displaying specific thumbnail size in single.php

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

这是我的 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; ?> 

有什么想法吗?

试试这个 ....

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

.....

您正在使用正确的函数来声明对 post 缩略图的支持并添加图像大小,但您需要在 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' );

此外,您使用了错误的函数来检查 post 是否有缩略图。它应该仍然有效,但这是正确的方法。

内部 single.php 变化:

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

收件人:

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

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