Take the 2-minute tour ×

So here is the scenario. I have to build my client side and he want to grab some categories to display on his homepage with its thumb. So my route is, just find the first post of that categories and get its thumbnail, but I don't know how to do this. I have follow this code http://stackoverflow.com/questions/15607840/display-first-last-post-from-each-categories-in-wordpress, but it will show all categories andd I don't know how to pull just certain (chosen) categories.

So The questions are:

  1. Is my route correct or you can suggest me another route or method.
  2. How to Grab first Image from some Chosen Categories

Here is the code I use to pull some chosen category.

$categories = get_categories('hide_empty=0&orderby=id');

foreach($categories as $category):
    $cat_field = 'rockable_cat_' . $category->cat_ID;
    if( //some argument return true )   
    $include .= '<div class="danboru"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></div>';

$include = substr($include, 0, -1); //Remove the last comma
return $include;

Solved..!! here is my full code

  1. grab the category name from category chosen

    function rockable_build_cat_exclude(){
        foreach($categories as $cat):
        $cat_field = 'rockable_cat_' . $cat->cat_ID;
        if( get_option($cat_field) and get_option($cat_field)=='false')
            $exclude .= "" . $cat->cat_name . ",";      
        $exclude = substr($exclude, 0, -1); //Remove the last comma
        return $exclude;
  2. Code to pull it in the front (index.php)

    $exclude = rockable_build_cat_exclude();
    $exclude_array = explode(",",$exclude);
    foreach ($exclude_array as $value){
        $cat_query = new WP_Query(array('category_name'=>$value,'showposts'=>1));
        if ($cat_query->have_posts()):while($cat_query->have_posts()):$cat_query->the_post();
        $category_id = get_cat_ID( $value );?>
        <div class="danboru">
            <?php the_post_thumbnail('home-thumb');?>
            <a href="<?php get_category_link( $category_id );?>" title="<?php echo esc_attr( sprintf( __( "View all posts in %s" ), $value ) ) ;?>"><?php echo $value;?></a>
        <?php endwhile;

Please share your thought if you have some suggestion or have some better solution.

share|improve this question
Your solution should be posted as an actual answer in the "answer" box below, and not as an edit to the question. –  s_ha_dum Nov 17 '13 at 15:54

1 Answer 1

Your solution seems overly complicated to me. WP_Query can do nearly all of the work, you just need to construct a meta_query to look for thumbnails. The code is slightly truncated but here is the idea:

$categories = array(1,2,3); // your specific category IDs
$first_thumb = new WP_Query(
    'posts_per_page' => count($categories),
    'category__in' => $categories,
    'meta_query' => array(
        'meta_key' => '_thumbnail_id',
        'compare' => 'EXISTS'
if ($first_thumb->have_posts()) {
  while ($first_thumb->have_posts()) {
share|improve this answer
Thanks.. your code looks cleaner than me.. You are right, the key is on meta_query and I just didn't know that WP has the _thumnail_id meta key. –  AgepRoem Nov 18 '13 at 2:51

Your Answer


