Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have some code that display's the titles of my child category posts on one of my pages:

<?php
    $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.

Thanks,
Josh

share|improve this question
add comment

2 Answers

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() ) :
                the_post();

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

                echo '<ul>';
                printf( '<li>%s<a href="%s">%s</a></li>',
                    $image,
                    get_category_link( $cat->term_id ),
                    apply_filters( 'get_term', $cat->name ) );
                echo '</ul>';
            endwhile;
        else:
            // CATEGORY HAS NO POSTS
        endif;
    }
} else {
    // CATEGORY DOESN'T EXIST
}
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
add comment

My final code looks like:

<?php
    $parent = get_cat_ID("photos");
    $cats = get_categories("child_of=".$parent);
    foreach ($cats as $cat) {
    query_posts("posts_per_page=1&orderby=rand&cat=".$cat->cat_ID);
?>
    <?php if ($parent == true && have_posts()) : while (have_posts()) : the_post(); ?>
        <ul>
            <?php if (has_post_thumbnail()) { ?>
                <?php
                    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 } ?>
        </ul>
    <?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 :)

Josh

share|improve this answer
add comment

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.