0
votes

I have a strange problem which I'm hoping is just due to me missing something obvious...

Our Wordpress installation has upgraded to 3.8.1 recently. We use WordPress within a directory on the site as part of our news items. We loop through posts in other sections of our site to show latest news etc

We have featured image enabled, and have the featured images showing on the wordpress/news home page by using the following code:

<a href="<?php the_permalink() ?>"><div class="snippet-featured-img"><?php the_post_thumbnail('thumbnail'); ?></div></a>

This works fine, and outputs the 150px by 150px featured image.

We also wish to show the featured image next to the posts we loop through on the other pages of the site, but we can't seem to get it working.

This is the code:

foreach($posts as $post) {
setup_postdata($post);
if ( has_post_thumbnail() ) {
echo "<a href='";
echo the_permalink();
echo "'>";
the_post_thumbnail(array('50','50'));
echo "</a>";
}
echo "<h2><a href=\"";
echo the_permalink();
echo "\" rel=\"bookmark\" title=\"Permanent Link to ";
echo the_title();
echo "\">";
echo the_title();
echo "</a></h2>\n<p>";
$string = strip_tags(strip_shortcodes($post->post_content));
echo chop_string($string,190,'...');
echo " <a href=\"";
echo the_permalink();
echo "\" rel=\"bookmark\" title=\"Permanent Link to ";
echo the_title();
echo "\">";
echo "Read More</a></p>\n";
echo "<div class='clear'></div>\n";
} 

The post var is declared a bit higher up the script:

$posts = get_posts('numberposts=8&orderby=date');

Everything except the image is outputted, even the link around where the image should be is outputted, indicating that the has_post_thumbnail() func has returned true. As I said, it's also working on the wordpress page itself.

I have tried with 'thumbnail' passed instead of the size array, and also get_the_post_thumbnail(), and I'm at a loss as to why it's not working!

Any help would be great

Thanks

2

2 Answers

0
votes

USe this one using get function.

<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>


    get_the_post_thumbnail($post_id);                  // without parameter -> Thumbnail

    get_the_post_thumbnail($post_id, 'thumbnail');     // Thumbnail
    get_the_post_thumbnail($post_id, 'medium');        // Medium resolution
    get_the_post_thumbnail($post_id, 'large');         // Large resolution
    get_the_post_thumbnail($post_id, 'full');          // Original resolution

    get_the_post_thumbnail($post_id, array(100,100) ); // Other resolutions

In your Custom Post Type, do you setup_postdata( $post ) in your custom Loop? If not, has_post_thumbnail() might not be defined/available?

EDIT:

Try adding:

setup_postdata( $post );

Right before:

$loop->the_post();

And then see if has_post_thumbnail() returns true?

Or, try passing the $post->ID to your call to has_post_thumbnail()?

has_post_thumbnail( $post->ID );
0
votes

Try changing

the_post_thumbnail(array('50','50'));

to

echo get_the_thumbnail($post->ID, array(50,50));