0

I want to retrieve the content of posts within a foreach loop.

        $recent_posts = wp_get_recent_posts();
        $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        $content = apply_filters( 'the_content', get_the_content() );
        $content = str_replace( ']]>', ']]>', $content );

        foreach( $recent_posts as $recent ){
            echo '<img src="'. $url .'">'. '<a href="'. get_permalink($recent["ID"]) .'"><h1>'. $recent["post_title"] .'</h1><h2>'. get_the_date() .'</h2><p>'. $content .'</p></a>';
        }

but it gives me an empty <p>. Is it not possible to use get_the_content(); like this?

Thanks

0
0

get_the_content(); must be used in the loop for it to work as specified in the Codex. To get post content in your case you have to use $recent["post_content"] instead.

1
  • cheers dude, its working fine! – user49869 Nov 7 '14 at 15:16

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.