【问题标题】:Can't change default thumbnail size无法更改默认缩略图大小
【发布时间】:2015-05-03 16:31:31
【问题描述】:

我正在 wordpress 中制作图库,我想更改默认缩略图大小,但它无法正常工作。当我在 wordpress 媒体选项中将照片设置为后缩略图自然大小为 150x150 时,我将缩略图大小更改为 215 143。

在我拥有的功能中

add_theme_support( 'post-thumbnails' );

if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 1240, 1240 );
}

我的画廊查询:

<ul id="stage">
                <?php
                // The Query
                $the_query = new WP_Query( array( 'post_type' => 'flota', 'orderby' => 'title', 'order' => 'ASC' ) );

                // The Loop
                while ( $the_query->have_posts() ) : $the_query->the_post();

                $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
                $full = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'full' );

                $cats = wp_get_object_terms( $post->ID, 'flota_category' );

                $items = array();
                    foreach ( $cats as $cat ){
                        $slug = $cat->slug;
                        $items[] = $slug;
                    }
                    $counter = count($cats);
                    $i = 0;

                ?>

                <li data-tags="<?php 
                    foreach ( $items as $tag ){
                        if (++$i === $counter ){
                            $tags = $tag;
                        }
                        else{
                            $tags = $tag . ', ';
                        }
                        echo $tags;
                    }
                ?>"><a href="<?php echo $full[0] ?>" rel="lightbox[flota]"><img src="<?php echo $thumbnail[0] ?>" width="215" height="143" style="background:#ffffff;"></a></li>

                <?php
                endwhile;               
                ?>      
</ul>

【问题讨论】:

    标签: php wordpress thumbnails


    【解决方案1】:

    在functions.php中使用:

    add_theme_support( 'post-thumbnails' );
    
    add_image_size( 'gallery-thumb', 215, 143 );
    

    您每页的默认帖子数为 10,这就是您只能看到 10 张图片的原因。通过将查询更改为:

    $the_query = new WP_Query( array( 'post_type' => 'flota', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1, ) );
    

    在您的查询中,将 $thumbnail 和 $full 替换为:

    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id(), 'gallery-thumb' );
    $full      = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
    

    然后使用http://wordpress.org/plugins/regenerate-thumbnails/ 之类的插件重新生成缩略图。

    在循环中,get_post_thumbnail_id 不需要设置 ID,但如果您决定在其他地方设置它,则您正在寻找 $post->ID 而不是 $the_query->ID。设置 $global $post;使用时也一样。

    【讨论】:

    • 为什么我不能添加超过 10 张照片?
    • @user3211948 我已经修改了我的答案,为你消除了这个限制。
    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 2014-09-17
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多