Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Atahualpa 3 Wordpress theme (http://forum.bytesforall.com/forumdisplay.php?f=2)
-   -   [SOLVED] Help using the_post_thumbnail (http://forum.bytesforall.com/showthread.php?t=12269)

Tim F Jan 16, 2011 06:46 AM

[SOLVED] Help using the_post_thumbnail
 
Hi all,

I am trying to set up a bit of code to show the latest post excerpt from a particular category. I want the post thumbnail to display if it is present in that post, and if not I want a default image to show. I am using this:

Code:

<?php global $post;
$myposts = get_posts('numberposts=1&category=8');
foreach($myposts as $post) :
?>
<a href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail( array(100,100) ); else echo '<img class="social" src="default.jpg" alt="No Thumbnail" title="No Thumbnail" />';
?><br>
<?php the_title(); ?></a>
<?php endforeach; ?>

I can't get it to load the post thumbnail and I am wondering - am I wrong in assuming that the 'set featured image' box in the post is supposed to be the thumbnail? Or should I be choosing the thumbnail somewhere else? Or is my code just borked? :p

lmilesw Jan 16, 2011 09:57 AM

I'm not sure where you are placing that code but I would use the List Category Posts Plugin for what you are trying to do and the Feaured Image function works fine.

Tim F Jan 17, 2011 05:20 AM

I am putting this in style and edit center column. I was hoping I could achieve it without using any more plugins as my site is starting to get a bit bloated with them.

So it sounds like featured image is what I need to choose the thumbnail, so there must be a problem with my code. I wonder if I actually need to specify which post has the thumbnail in my if statement? But that would make the whole thing pretty redundant.

Tim F Jan 17, 2011 05:51 AM

Solved - I needed to add this: setup_postdata($post);

So the whole thing now reads

Code:

<?php global $post;
$myposts = get_posts('numberposts=1&category=13');
foreach($myposts as $post) :
setup_postdata($post);
?>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) the_post_thumbnail( array(100,100) ); else echo '<img border=0 class="social" src="defaultimage.jpg" alt="No Thumbnail" title="Example" />';
?><br>
<?php the_title(); ?></a>
<?php endforeach; ?>

And when added to 'style and edit center column' it shows the latest post title from category 13 and the thumbnail if there is one, or a default image if not. I set up multiple instances in a table to show posts from several of my categories on the home page.


All times are GMT -6. The time now is 11:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.