Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently using a custom made Twentytwelve child theme and would like to display the last 5 posts on the homepage using a shortcode. I have tried using the code below (added to functions.php) but to no avail - when I add the shortcode to the homepage, it doesn't display anything although as far as I can see, it should and I have a test post live.

function custom_prev_posts($limit = 5){
global $wpdb, $post;
$html = '';
$prev_posts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_date < '%s' AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date DESC LIMIT $limit", $post->post_date ) ); 

if($prev_posts){
$html .= '<ul>';
foreach ( $prev_posts as $prev_post  ) {
$html .= '<li><a href="' . get_permalink( $prev_post->ID ) . '">' .$prev_post->post_title . '</a></li>';
}
$html .= '</ul>';
}
return $html;
}

add_shortcode('last5', 'custom_prev_posts');

I'm thinking the issue may be related to the Twentytwelve theme. Any ideas what I'm doing wrong or does anyone know of a working way to display the latest 5 posts inside a page?

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

The issue is with your $limit var, you can't set a default value like that because WordPress overwrites it with an empty value. Check out the Shortcode API for the correct way to pass and set shortcode attributes.

function custom_prev_posts( $atts ){
    extract( shortcode_atts( array(
        'limit' => 5
    ), $atts ) );
    // the rest of your code, $limit now works correctly
}

You can also now set a custom limit by adding limit to your shortcode: [last5 limit=10] Though last5 is an odd name for a shortcode that can display numbers of posts other than 5 :)

Also, for future reference, enable debugging so you can see errors your code is generating.

share|improve this answer
    
Thanks, worked perfectly. Also thanks for the links, very useful and very much appreciated. –  Nikki Mather Feb 15 '13 at 16:38
add comment

You can try this:

function custom_prev_posts($limit){
    extract( shortcode_atts( array(
    'limit' => 5,
    ), $atts ) );

    // check input paramters
    $limit = (int)$limit; // allow only numbers

    // output
    $html = '';
    $html .= '<ul>';
    $args = array('numberposts' => $limit);
    $recent_posts = wp_get_recent_posts($args);
    foreach( $recent_posts as $recent ){
        $html .= '<li><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    }
    $html .= '</ul>';
    return $html;
}
add_shortcode('last5', 'custom_prev_posts');

where I use wp_get_recent_posts(), you can read more about it in the Codex:

http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

Edit: You should definitely check out the link to the shortcode api provided by @Milo

share|improve this answer
    
Hey, code worked great as did Milo's, thanks! –  Nikki Mather Feb 15 '13 at 16:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.