【问题标题】:Sizing images to thumbnail size in WordPress在 WordPress 中将图像大小调整为缩略图大小
【发布时间】:2011-02-15 23:12:33
【问题描述】:

我正在构建我的第一个 WordPress 主题,但我遇到了一些问题。

我的functions.php 中有一个名为get_first_photo() 的函数,它抓取每篇文章上传的第一张图片并将其放在博客存档页面上。它工作正常,但它会加载全尺寸图像并使用 CSS 调整其大小。我宁愿它以在 WP 控制面板中设置的缩略图大小加载图像,这样我就没有图像大小的开销。有什么办法可以做到这一点?

这是来自functions.php的代码:

function get_first_photo($id) {
    global $wpdb;
    return $wpdb->get_var("SELECT guid FROM wp_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");  
}

这是博客模板:

<?php

get_header(); ?>
<div id="content" class="blog">
    <div id="body">
        <h3 class="title" id="blog">The Ned Leary Blog</h3>
<?php if (have_posts()) : 
query_posts("category_name=blog");
while (have_posts()) : 
the_post(); 
$first_photo = get_first_photo(get_the_ID());
?>
        <div class="snippet">
<?php if (!empty($first_photo)) : ?>
            <img src="<?php echo $first_photo; ?>" alt="Thumbnail" />
<?php else : ?>
            <img src="<?php bloginfo('url'); ?>/images/blog/gravatarBig.jpg" alt="Ned Leary Gravatar" />
<?php endif; ?>
            <div class="details">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <h5><?php the_time('D, M j, Y') ?> by <?php the_author('') ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></h5>
                <?php the_excerpt(); ?>
                <h4><a href="<?php the_permalink(); ?>">Read more&hellip;</a></h4>
            </div>
        </div><!--end snippet-->
<?php endwhile; endif;// end of the loop. ?>
    </div><!--end body-->
<?php get_sidebar(); ?>
</div><!--end content-->
<?php get_footer(); ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您只需要获取您想要的第一张图片的帖子 ID,然后通过“get_the_post_thumbnail()”运行它。

    $first_photo = post id of the first image;    
    echo get_the_post_thumbnail( $first_photo );
    

    如果您愿意,也可以拉出您自己的自定义缩略图大小。只要你使用的是wordpress 2.9+。

    只需将其添加到functions.php

    add_theme_support( 'post-thumbnails' ); //enable thumbs
    add_image_size( 'mycustom-preset', width you want, height you want); //custom size
    

    然后运行相同的函数,但调用你的新预设...

    $first_photo = post id of the first image;    
    echo get_the_post_thumbnail( $first_photo, 'mycustom-preset' );
    

    http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

    http://codex.wordpress.org/Function_Reference/add_image_size

    希望对您有所帮助。您似乎在获取第一张照片的帖子 ID 时没有遇到问题,所以我没有真正介绍如何获取它。

    【讨论】:

    • 这很接近,但需要我在每个帖子上设置帖子缩略图,这是我试图避免的。
    【解决方案2】:

    您可以使用GD 或其他一些图像库以编程方式操作图像。但是,最好在上传图像时执行创建缩略图文件的操作。您可以让它在每次加载页面时动态生成缩略图,但是这样的操作可能会相对计算量大,并且会给系统带来不必要的负载。

    【讨论】:

      【解决方案3】:

      感谢 John Ford 为我指明了正确的方向,我能够弄清楚这一点。

      我的函数中的查询略有变化,获取的是帖子 ID 而不是 guid。

      function get_first_photo($id) {
          global $wpdb;
          return $wpdb->get_var("SELECT id FROM aire_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");  
      }
      

      然后我不得不在我的主题文件中添加另一行:

      $first_photo = get_first_photo(get_the_ID());
      $thumb = wp_get_attachment_image_src($first_photo);
      

      最后,我更新了我的图片src:

      <img src="<?php echo $thumb[0]; ?>" alt="Thumbnail" />
      

      【讨论】:

        【解决方案4】:

        您可以使用 get_the_post_thumbnail 函数或使用 php 缩略图生成器,如 timbthumb

                    <?php $images = get_children( array( 'post_parent' => $page->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
                if ( $images ) {
                    $total_images = count( $images );
                    $image = array_shift( $images );
                    $thumbnail = wp_get_attachment_image_src($image->ID, array(225,125) );
                    $thumbnail = $thumbnail[0]; }
                    ?>
                     <img class="size-thumbnail alignleft" src="<?php echo $thumbnail; ?>" alt="<?php echo $page->post_title ?>">
        

        【讨论】:

          猜你喜欢
          • 2012-01-06
          • 2015-04-03
          • 2011-10-22
          • 2015-07-16
          • 2011-09-03
          • 2015-05-25
          • 2012-05-25
          • 2014-10-29
          • 1970-01-01
          相关资源
          最近更新 更多