Support » Themes and Templates » Recent Post Thumbnail with Categories

  • I recently created a new theme and learned how to insert a recent post thumbnail into a template page. It works fine, but I’m wondering if anyone knows how I can alter the code to only show posts from a particular category. This code is in the functions.php file and is as follows:

    if ( function_exists( 'add_theme_support' ) ) {
            add_theme_support( 'post-thumbnails' ); } //Adds thumbnails compatibility to the theme
    set_post_thumbnail_size( 300, 200); // Sets the Post Main Thumbnails
        add_image_size( 'recent-thumbnails', 375, 250); // Sets Recent Posts Thumbnails
    function recentPosts() {
    	$rPosts = new WP_Query();
    		while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
    				<a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a>
    		<?php endwhile;

    And in the template file the function is called with the following:
    <?php echo recentPosts(); ?>

    Any help would be great!

Viewing 2 replies - 1 through 2 (of 2 total)
  • HI

    You can call the function in template file like

    <?php query_posts(‘cat=8& posts_per_page=5’);
    while ( have_posts() ) : the_post();
    $thumb_image_url = wp_get_attachment_url(get_post_thumbnail_id($post->ID), ‘thumbnail’);

    ?><img src=”<?php echo $thumb_image_url; ?>” alt=”<?php the_title(); ?>” width=”145″ height=”105″/>
    this one help you in easy way

    Hmm, I tried that code but when I loaded the page a got a syntax error.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Recent Post Thumbnail with Categories’ is closed to new replies.