WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get a page to list all the child categories and then display their name and a thumbnail from the most recent post in that category.

This is the following code I've got, it appears to be fetching some thumbnails from other posts that aren't in the category and also repeating the categories.

Any help would greatly be appreciated.

<?php get_header(); ?>

        <div id="left_full">

        <?php
$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'child_of' => '3592'
  );
$categories = get_categories($args);
  foreach($categories as $category) {
        query_posts( $category->name . 'posts_per_page=1' );
        while ( have_posts() ) : the_post();
    echo '<div class="interview_item">';
    echo '<div class="interview_thumb"><a href="#" rel="bookmark">'; the_post_thumbnail('interview-thumb'); echo '</a></div>';

    echo '<h1><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h1> ';
    echo '</div>';
    endwhile;
    wp_reset_query();
    }
?>

        </div>

    </div>

<?php get_footer(); ?>
share|improve this question

Did some playing about, managed to get it working.

 <?php
/*
Template Name: Festivals - United States
*/
?>
<?php get_header(); ?>

        <div id="left_full">

        <?php
$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'child_of' => '3592'
  );
$temp_query = $wp_query;
$categories = get_categories($args);
  foreach($categories as $category) {
query_posts("posts_per_page=1&cat=$category->cat_ID");
if (have_posts()) : while (have_posts()) : the_post();
            echo '<div class="interview_item">';
    echo '<div class="interview_thumb"><a href="#" rel="bookmark">'; the_post_thumbnail('interview-thumb'); echo '</a></div>';

    echo '<h1><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h1> ';
    echo '</div>';
endwhile; endif;
    }
$wp_query = $temp_query;
?>

        </div>

    </div>

<?php get_footer(); ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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