2

I am using a homepage sorter to arrange the posts on the front page of my site because of that, I am getting the posts in this manner so that I can call each post individually instead of in a foreach loop

    $args = array( 'numberposts' => -1,'category_name' => 'homepage');
$home_post = get_posts( $args );
$image = get_the_post_thumbnail($post->ID);
global $more;
$more = 0;

Then I can call each post this way

<?php if( isset( $home_post[1] ) ) { ?>
 <p><?php echo $home_post[1]->post_excerpt; ?></p>
 <?php } ?>

My problem is that get_posts has no way to get the post thumbnail outside of using a loop. I can't use a loop because I need my post called in a switch statement. I can't find a way to get the post thumbnail. Do you know of a way?

1

get_the_post_thumbnail() expects the post ID as first parameter. So you need a post object like … $home_post[1].

echo get_the_post_thumbnail( $home_post[1]->ID );

… should do what you need.

  • 2
    Although I do appreciate your assistance with my issue, I feel as though any good will you earned was colored by your comment above. Please do not assume that a person can achieve any goal just because someone else wrote a tutorial on the subject. I did try passing the ID to the post, I just did it wrong. I am still trying very hard to learn php and people who know it can make it easier or harder. Try to be the good sport and make it easier. Your answer did give me an Aha moment and I did learn something. So for that, I thank you. – Jamie Aug 22 '12 at 22:59
  • 2
    @Jamie Please don’t get me wrong. Everybody has to start somewhere, I respect that. But I have to keep in mind how any question can help future visitors. Very localized or simple questions rarely do that. The core of this problem is: Every variable with an -> is an object. That’s not even WordPress specific. :) Besides that … English is my third language. I might sound harsh sometimes unintentionally. – fuxia Aug 23 '12 at 1:33
  • @Jamie I deleted the comment form my friend Fuxia. Btw, the issue (which she explained here) is that this site is not a PHP tutorial site, but a QA site on WordPress. Because I know Fuxia, I can assure you that her suggestion to spend time on a PHP tutorial was meant to help you, not to critic. Because if you are willing to learn PHP and WordPress, asking very basic questions (and waiting for answers) very likely is not the best / fastest way to do it. If you spend some time on some PHP tutorial (or even book, if you can) you will learn better and faster. – gmazzap Sep 17 '18 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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