PDA

View Full Version : How to display recent posts in wordpress



A Khan
05-18-2014, 05:39 AM
Function Reference/wp get recent posts
Description

Retrieve the most recent posts.

Usage


<?php wp_get_recent_posts( $args, $output ) ?>
Default Usage


<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );

$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
Return Value


$posts (array)
List of post arrays (default) or objects depending on $output

Parameters


$args
(array) (optional)

Default: array

$output
(string) (optional) Constant OBJECT, ARRAY_A

Default: ARRAY_A


Examples

This is an example that shows how to use the wp_get_recent_posts() function to list the recent 10 posts.


<h2>Recent Posts</h2>
<ul>
<?php
$recent_posts = wp_get_recent_posts();
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>

If you want to delimit more or less recent posts you have to put the number in the function parameter like this example below:


<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>

Lienia henna
05-18-2014, 05:44 AM
Good thread. I am using this code to display recent posts on my blog.


<div style="width:100%;max-width:986px; height:17px; border:#808080 2px groove; background-color:#FFCC00"> <marquee onmouseover="this.stop();" onmouseout="this.start();" scrollamount="5" width="100%>
<div><font face="Microsoft Sans Serif" style="font-size: 12px;"><?php


$args = array( 'numberposts' => '7' , 'post_status' => 'publish');
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<img src="wp-includes/images/upcric-marquee.png"> <a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" style="color:#006" >' . $recent["post_title"].'</a> ';
}


?></font></div>
</marquee>


</div>

Abm House
07-14-2014, 05:04 AM
<ul>





2
<?php $the_query = new WP_Query( 'showposts=5' ); ?>





3






4
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>





5
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>





6






7
<li><?php echo substr(strip_tags($post->post_content), 0, 250);?></li>





8
<?php endwhile;?>





9
</ul>