Nick Shaw

WordPress Display last post and image from specific category

Used to display the last post and image from a specific category, this can be useful for latest news on a homepage or latest posts in a sidebar.

<?php
query_posts('cat=3&#038;showposts=1'); // replace 123 by category ID of "Headline"  
if(have_posts()) : the_post(); $images = get_children(array( 'post_parent' =  --> get_the_id(),
'post_type' =&gt; 'attachment',
'post_mime_type' =&gt; 'image',
'orderby' =&gt; 'ID',
'order' =&gt; 'ASC',
'numberposts' =&gt; 1
));
foreach((array)$images as $key =&gt; $image){
echo wp_get_attachment_image($key);
}
the_title();
the_excerpt();
endif;
wp_reset_query();
?>
Exit mobile version