Barry-O-Brien
Member
Posted 1 year ago #
I want to show a list of new articles from one category but only have one thumbnail show from the most recent article in that list, can anyone help?
This is the code I have so far, div id="p" is where I need the thumbnail.
<div id="rapsbox">
<div id="lfttxt"><div id="t-t"></div><div id="p"></div><div id="rapboxtxt">
<?php $args = array( 'numberposts' => 9, 'category' => 7);
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<div id="txtinr-rr"><a href="<?php the_permalink() ?>" class="blue" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></div><?php endforeach; ?>
</div></div>
for example:
<div id="p"><?php $ctp = get_posts( array( 'posts_per_page' =>1, 'category' => 7, 'meta_key' => '_thumbnail_id' ) ); if( $ctp ) foreach( $ctp as $cp ) { echo get_the_post_thumbnail($cp->ID,'medium'); }; ?></div>
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
adjust the size parameter for the thumbnail if needed.
Barry-O-Brien
Member
Posted 1 year ago #
Thank you for that, it works to some degree but the wrong thumbnails are showing in the wrong area's "not matching up to the correct category"
http://ziovex.com
Any idea's?
not matching up to the correct category
what thumbnail is wrong and what would you expect in its place?
do all of the posts have thumbnails?
can the posts with their thumbnails be seen somewhere else on your site?
as you can see, the thumbnail code uses the same category as your other code;
are you using various versions of that code for the 'economy', 'law' etc areas?
what is the full code of that template?
(use the pastebin for pasting the code)
Barry-O-Brien
Member
Posted 1 year ago #
Yes is just the bottom sections economy, law etc
http://pastebin.com/4U5ehq7m
my suggested code has $cp->ID in this section:
echo get_the_post_thumbnail($cp->ID,'medium');
you changed it wrongly to $id in your code:
echo get_the_post_thumbnail($id);
leaving the thumbnail size away is ok -
the line should look like this in your code:
echo get_the_post_thumbnail($cp->ID);
Barry-O-Brien
Member
Posted 1 year ago #
Yes that fixed it, sorry about that, I changed it because I thought it would resize the thumbnail, I was following the code reference.
no problem - please mark the topic as 'resolved' if this is fixed.