【问题标题】:Displaying specific thumbnail size in single.php在 single.php 中显示特定的缩略图大小
【发布时间】:2015-05-24 16:55:29
【问题描述】:

目前我正在构建一个 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; ?> 

有什么想法吗?

【问题讨论】:

    标签: wordpress thumbnails


    【解决方案1】:

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

    【讨论】:

    • 感谢 Nathan,这似乎有效,感谢您的帮助。
    【解决方案2】:

    试试这个 ....

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

    ....

    【讨论】:

    • 这是不正确的,原因有很多。 the_post_thumbnail 用于输出缩略图并且不返回值,因此 echo 是多余的。 the_post_thumbnail 不接受帖子 ID 作为其参数之一。您用双引号打开缩略图大小字符串并用单引号关闭它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2014-07-24
    相关资源
    最近更新 更多