andy
andy

Reputation: 2754

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 &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
            </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.

Upvotes: 0

Views: 8788

Answers (3)

user3066412
user3066412

Reputation: 1

with the new wordpress versions you can setup thumbnails from settings > media . and give the personnal size to thumbnail Then use this to get the thumbnail with your prefered size

<?php the_post_thumbnail('thumbnail');?>

Upvotes: 0

Anirudh babbar
Anirudh babbar

Reputation: 92

<?php 
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
  the_post_thumbnail();
} 
?>

Add the above code in loop

Then add the following code to functions.php

add_theme_support( 'post-thumbnails' ); 

Then at last, if you wish to link your thumbnail to post id, so your posts opens after clicking image, add the following code to functions.php

set_post_thumbnail_size( 50, 50 );
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

function my_post_image_html( $html, $post_id, $post_image_id ) {

  $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
  return $html;

}

set_post_thumbnail_size( height, width); this is used to add height and width, in above example i added 50, 50. Change it with your required value

Upvotes: 1

lucentx
lucentx

Reputation: 161

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

?>

Upvotes: 1

Related Questions