Skip to content
Advertisement

WordPress – post thumbnail in loop

So I’d like to add a thumbnail to my posts but I just can’t get it to work.

<?php get_header(); ?>

<div id="main-content">
    <?php get_sidebar(); ?>
    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts('posts_per_page=3&paged=' . $paged);
    ?>
    <?php if (have_posts()) : while ( have_posts()) : the_post(); ?>
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
            <?php the_post_thumbnail();?>

            <div class="entry">
                <?php the_excerpt(); ?>
                <a class="read-more" href="<?php the_permalink() ?>">Read More ...</a>
            </div>

            <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

            <div class="postmetadata">
                <?php the_tags('Tags: ', ', ', '<br />'); ?>
                Posted in <?php the_category(', ') ?> |
                <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
            </div>
        </div>
    <?php endwhile; endif; ?>

    <div class="navigation">
        <div class="next-posts"><?php next_posts_link('&laquo; Older Posts') ?></div>
        <div class="prev-posts"><?php previous_posts_link('Newer Posts &raquo;') ?></div>
    </div>
</div>
<!-- end div main-content -->

<?php get_footer(); ?>

And in my functions.php I’ve added – add_theme_support('post-thumbnails');

It gives me the option to post the thumbnail when I make a post, but it doesn’t show up.

Advertisement

Answer

What theme or parent theme are you using? I usually do something like this inside the loop:

<?php

if ( function_exists( 'add_image_size' ) ) {
  add_image_size( 'custom-thumb', 180, 115, true ); //add a custom image size
}

echo get_the_post_thumbnail(get_the_ID(), 'custom-thumb', $attr); //echo the thumbnail with the new custom image size

?>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement