0

I want make a button to filter posts from a specific category, But also i would like this button to display the featured image of the latest post in that category.

I'm completly new to php and wordpress, but i tought maybe using the get_category and get_thumbnailfunctions may be a good way to go.

What do you guys think? I am sorry for the vagueness, but I am a little lost about this. Thanks for any hints in advance!

1 Answer 1

1

Try this:

<?php 
    $the_query = new WP_Query( 'showposts=1&cat=55' );
    if($the_query->have_posts()) : 
        while ($the_query -> have_posts()) : $the_query -> the_post();?>
          <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail();?></a>
 <?php endwhile;
    endif;
    /* Restore original Post Data */
    wp_reset_postdata();?>

Replace cat=55 with the category ID you want and/or replace showposts=1 with the number of latest posts you want to show if you want more than 1.

If you want a different size for your thumbnail, you can use one of the followings (taken from http://codex.wordpress.org/Function_Reference/the_post_thumbnail):

the_post_thumbnail();                  // without parameter -> 'post-thumbnail'
the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
the_post_thumbnail('large');           // Large resolution (default 640px x 640px max)
the_post_thumbnail('full');            // Full resolution (original size uploaded)
the_post_thumbnail( array(100,100) );  // Other resolutions
7
  • I can use this code wherever in the page? or do i have to paste it in a specific file? (sorry for the total ignorance)
    – invicente
    Commented May 20, 2014 at 1:26
  • @invicente where are you trying to show this?
    – renny
    Commented May 20, 2014 at 3:17
  • in the main home
    – invicente
    Commented May 20, 2014 at 3:37
  • just paste it into the template file where you want it to show up.
    – renny
    Commented May 20, 2014 at 3:47
  • even though IT IS working, it's failing to recognize the specific ID of the category I chose. it always displays the thumbnails from the same cat, with any number.... what could be the problem?
    – invicente
    Commented May 20, 2014 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.