0

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

2 Answers 2

2

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
}
4
  • 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? Jan 10, 2013 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. Jan 11, 2013 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!! Jan 11, 2013 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! Jan 11, 2013 at 19:26
0

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

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.