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'm trying to show some Wordpress category thumbnails on my homepage, but it seems my code is breaking the rest of my site. It kills the page and anything under my code won't load.

Any help would be appreciated on how to get this working.

Thanks!

<?php

    global $post;
    $temp_post = $post;
    $args = array('numberposts' => 1, 'category' => 40, 'orderby' => 'date', 'order' => 'DESC');
    $posts = get_posts($args);
    foreach ($posts as $post) : setup_postdata($post);
    if (has_post_thumbnail()) {
        $thumbnail = get_post_thumbnail($post->ID, 'thumbnail');
        $title = get_the_title();
        $output = $title . "<br />" . $thumbnail;
    }
    endforeach;
    $post = $temp_post;
    wp_reset_postdata();
    return($output);

?>

EDIT

<?php

global $post;
$temp_post = $post;
$args = array('numberposts' => 6, 'category' => 227, 'orderby' => 'date', 'order' => 'DESC');
echo '<div class="categoryThumbnailList">';
$posts = get_posts($args);
foreach ($posts as $post) : setup_postdata($post);
if (has_post_thumbnail()) {
    $thumbnail = get_the_post_thumbnail($post->ID, 'thumbnail');
    $link = get_permalink($post->ID);
    $title = get_the_title();
    echo '<div class="categoryThumbnailList_item">';
    echo '<a href="' .$link . '" title="' .$title . '">' .$thumbnail . '</a><br/>';
    echo '<a href="' .$link . '" title="' .$title . '">' .$title . '</a>';
    echo '</div>';
}
endforeach;
echo '</div>';
echo '<div class="categoryThumbnailList_clearer"></div>';
$post = $temp_post;
wp_reset_postdata();

?>

share|improve this question

migrated from stackoverflow.com May 5 '12 at 18:50

This question came from our site for professional and enthusiast programmers.

    
Your code appears to be trying the get the post's thumbnail with get_post_thumbnail(), not a category thumbnail. Are you using a plugin to set up category thumbnails? That would be a useful piece of information. –  mrwweb May 5 '12 at 20:01
1  
Got it. It should be get_the_post_thumbnail() - I forgot the –  McGuyverr May 5 '12 at 20:12
    
That still will get the thumbnail on a POST not a CATEGORY? Maybe I just misunderstood your question. If you've resolved your issue, please submit your answer and mark it as the right answer. –  mrwweb May 5 '12 at 20:14
    
I'm trying to put this on my homepage where it already has a blog and loading posts with the_posts() - But my code takes place of the regular blog posts. How can I fix that? –  McGuyverr May 5 '12 at 20:57
    
Is it fair to say your question is "How to display the thumbnail for posts in a category?" –  mrwweb May 5 '12 at 21:38
show 1 more comment

1 Answer 1

The problem is your use of return is not appropriate here, it's stopping execution of the template. You should echo your output.

share|improve this answer
    
thanks that helped a little bit. But it's still only outputting commas. –  McGuyverr May 5 '12 at 19:16
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.