Results 1 to 3 of 3

Thread: How to display recent posts in wordpress

  1. #1
    Moderator A Khan's Avatar
    Join Date
    Nov 2012
    Posts
    176
    ΑΡ Credit
    1760

    How to display recent posts in wordpress

    Function Reference/wp get recent posts
    Description

    Retrieve the most recent posts.

    Usage


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

    Code:
    <?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

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

    Parameters
    Code:
    $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.
    Code:
    <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:
    Code:
    <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>

  2. #2
    Moderator Lienia henna's Avatar
    Join Date
    Aug 2013
    Location
    Pak
    Posts
    118
    ΑΡ Credit
    1180
    Good thread. I am using this code to display recent posts on my blog.
    Code:
    		<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>

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    ΑΡ Credit
    70
    <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>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •