WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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 have some code that display's the titles of my child category posts on one of my pages:

    $parent = get_cat_ID("photos");
    $cats = get_categories('child_of='.$parent);
    foreach ($cats as $cat) {
        echo "<ul>";
        echo sprintf("<li><a href='%s'>%s</a></li>", get_category_link($cat->term_id), apply_filters("get_term", $cat->name));
        echo "</ul>";
} ?>

Is there a way to also post a random featured image from the child category posts? I'm pretty sure I can use this existing code, just not sure what to change to make it work.


share|improve this question
up vote 2 down vote accepted

You can get a random post by category by using the following code:

query_posts( array (
    'showposts' => 1,
    'orderby'   => 'rand',
    'cat'       => $cat->term_id
) );

if ( have_posts() ) : while ( have_posts() ) : the_post();

And then use get_the_post_thumbnail() to retrieve the post featured image:

if ( has_post_thumbnail() )
    $image = get_the_post_thumbnail( $post_id );

Your final code could look like this:

$parent = get_cat_ID( 'photos' );

if ( is_category( $parent ) ) {
    $cats = get_categories( 'child_of=' . $parent );

    foreach ( $cats as $cat ) {
        query_posts( array (
            'showposts' => 1,
            'orderby'   => 'rand',
            'cat'       => $cat->term_id
        ) );

        if ( have_posts() ) :
            while ( have_posts() ) :

                $image = '';
                if ( has_post_thumbnail() )
                    $image = get_the_post_thumbnail( get_the_ID() );

                echo '<ul>';
                printf( '<li>%s<a href="%s">%s</a></li>',
                    get_category_link( $cat->term_id ),
                    apply_filters( 'get_term', $cat->name ) );
                echo '</ul>';
            // CATEGORY HAS NO POSTS
} else {
share|improve this answer
This is awesome!! I re-wrote it a little bit, but it's the same code...my code looks like: Pastebin - the only thing is that my else doesn't quite work...if I add a category that doesn't exist It shows all posts, I expected that I'd get a Sorry, no posts matched your criteria error. Any ideas why that doesn't work? – Josh Rodgers Jan 10 '13 at 19:50
get_cat_ID() returns 0 if the category doesn't exist. If that happens get_categories is asked to get all child categories with parent 0. So, all main categories will be queried. check the existence of the returned value of get_cat_ID() with is_category(). I added this check in my answer. – Mike Madern Jan 11 '13 at 8:29
Ok, if I remove the category I get the Category doesn't exist error, but if the category does exist - just has no posts - I still get the category doesn't exist error. I copied your code word for word just echoed your comments so that I could see what was happening. Almost there!! – Josh Rodgers Jan 11 '13 at 15:47
Ok, we're getting close...not sure why the else didn't work, but essentially I created my own else by adding a check for the child category. My code looks like: pastebin.com/ZW7Sb3H9. The only thing that doesn't work is If the category doesn't exist I should get a: Sorry, no posts matched your criteria error. Everything else is working great! – Josh Rodgers Jan 11 '13 at 19:26

My final code looks like:

    $parent = get_cat_ID("photos");
    $cats = get_categories("child_of=".$parent);
    foreach ($cats as $cat) {
    <?php if ($parent == true && have_posts()) : while (have_posts()) : the_post(); ?>
            <?php if (has_post_thumbnail()) { ?>
                    printf( '<li><a href="'.get_category_link($cat->cat_ID).'">'.get_the_post_thumbnail(get_the_ID(), "album-thumb").'</a><a href="'.get_category_link($cat->term_id).'">%s</a></li>',
                    apply_filters('get_term', $cat->name));
            <?php } ?>
    <?php endwhile; endif; } ?>
    <?php if (! $parent == true || ! $cat == true) { ?>
        <p><?php _e("Sorry, no posts matched your criteria."); ?></p>
    <?php } ?>
<?php wp_reset_query(); ?>

Basically, in this version a couple of things changed:

  1. I changed the if to make sure that the parent category exists and that we have a post, if both of those conditions are met then it will display my content...if not, it does nothing.

  2. Since the else wasn't working I wrote my own...that says...if the parent category or the child category doesn't exist than display my error message...if so, than do nothing.

Not sure why the else doesn't work, but my guess is it's because of the foreach that's the only thing that's different in this loop, that's not in most other loops.

Hope this helps someone else :)

Thanks Mike for all your help!!

*P.S. - I will be marking your solution as the answer, as it did answer my initial question :)


share|improve this answer

Your Answer


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.