WordPress.org

Ready to get started?Download WordPress

Forums

Recent Post Thumbnail with Categories (3 posts)

  1. fonsolo
    Member
    Posted 1 year ago #

    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();
    	$rPosts->query('showposts=9');
    		while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
    			<li>
    				<a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a>
    			</li>
    		<?php endwhile;
    	wp_reset_query();
    }

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

    Any help would be great!

  2. p5systems
    Member
    Posted 1 year ago #

    HI

    You can call the function in template file like

    <?php query_posts('cat=8& posts_per_page=5');
    while ( have_posts() ) : the_post();
    the_title();
    $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

  3. fonsolo
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic