2010-10-20 214 views
0

我正在构建我的第一个WordPress主题,并且我被卡住了。将图片大小缩小到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(); ?> 

回答

0

感谢约翰福特指出我在正确的方向,我能够弄清楚这一点。

我的函数中的查询稍有变化,抓取了帖子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" /> 
0

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

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,所以我没有真正涵盖如何获得它。

+0

This接近,但要求我在每个帖子上设置缩略图,这正是我想要避免的。 – Marcus 2010-10-21 13:24:29

0

您可以使用get_the_post_thumbnail功能或用途像timbthumb这样的php缩略图生成器

  <?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 ?>">