0

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(); ?>

1 Answer 1

1

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(); ?>

Your Answer

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

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