Php 在WordPress中将图像大小调整为缩略图大小

Php 在WordPress中将图像大小调整为缩略图大小,php,wordpress,Php,Wordpress,我正在构建我的第一个WordPress主题,我被一些东西卡住了 我在functions.php中有一个名为get_first_photo()的函数,它会获取每篇文章中上传的第一张图像,并将其放在博客存档页面上。它工作得很好,但是它加载全尺寸的图像并使用CSS调整大小。我宁愿它加载在可湿性粉剂控制面板中设置的缩略图大小的图像,这样我就不会有图像大小的开销。有没有办法做到这一点 下面是functions.php中的代码: function get_first_photo($id) { glo

我正在构建我的第一个WordPress主题,我被一些东西卡住了

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

下面是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(); ?>

Ned Leary博客
“alt=”缩略图“/>
/images/blog/gravatarBig.jpg“alt=“Ned Leary Gravatar”/>
由|

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

您只需获取所需第一幅图像的帖子ID,然后通过“获取帖子缩略图()”运行它

如果你喜欢,你也可以拉你自己的自定义缩略图大小。只要您使用的是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' );


希望有帮助。看来你在获取第一张照片的post id时并没有遇到问题,所以我没有真正介绍如何获取它。

多亏约翰·福特为我指出了正确的方向,我才能够解决这个问题

my函数中的查询发生了轻微变化,获取的是帖子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" />
“alt=”缩略图“/>

您可以使用get\u the\u post\u缩略图函数或使用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 ?>">

“alt=”“>

这很接近,但要求我在每篇文章上设置文章缩略图,这正是我试图避免的。
            <?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 ?>">