0

I have navigation tabs which shows recent posts and some category posts. I manage to successfully implement recent posts by using $recent_posts = wp_get_recent_posts(array( 'numberposts' => 9,)); and iterating over it by doing:

foreach( $recent_posts as $recent ){
    $image = get_the_post_thumbnail($recent["ID"]);
}

Now for category posts I tried to do $news_posts = get_posts(array( 'numberposts' => 9, 'category_name' => 'news',)); and since it returns array of objects:

    foreach( $news_posts as $temp_news ){
        $news = $temp_news.get_post();
        $image = get_the_post_thumbnail($news["ID"]);
}

But this is not working. I need $news in a format such that $news["ID"] works because I want to reuse the code. I tried many things and read many sample but nothing works.

Thanks in advance as I am quite novice with PHP and am pretty sure there must an easy solution.

0

You have mistake here:

.....
$news = $temp_news.get_post();

remove .get_post() from there.

  • It is not working. I have already tried that. Does wp_get_recent_posts() and get_posts() have same return type? I think first one is array of posts while other is array of post objects. Is this creating problem ? – Sid Nov 17 '16 at 9:00

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.