I want to know how to use WordPress get recents posts inside the post area?
I have this code from the WordPress website for getting the recent posts:
wp_get_recent_posts( $args, $output);
If i echo this function inside the post page body (place where i write my post) i get only the exact php code showing as a text?
<h2>Recent Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
The other code for showing the recent 5 posts also render to the post page as text and i don’t know why?
How to use it correctly?
I’m not certain about what you mean with “post area”. With “string text output” I presume you mean unformatted text links in a list.
If you need to have more control over how to format the output (to make it more like a regular post listing for example), use a regular WP Query for this. You can get the 5 latest blog entries with these arguments:
And to loop through them just use the regular WordPress main loop structure: