treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Display date in recent post function

  • I am using the Recent Posts Function from the Snippets section of CSS-Tricks, and it is working great, but how do I add the date into the mix?

    The function:

    function recent_posts($no_posts = 10, $excerpts = true) {

    global $wpdb;

    $request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";

    $posts = $wpdb->get_results($request);

    if($posts) {

    foreach ($posts as $posts) {
    $post_title = stripslashes($posts->post_title);
    $permalink = get_permalink($posts->ID);

    $output .= '<div class="home-post"><h1>' . htmlspecialchars($post_title) . '</a></h1>';

    if($excerpts) {
    $output.= '<p>' . stripslashes($posts->post_excerpt) . '</p>';
    }

    $output .= '<a class="read-more right" href="' . $permalink . '">Read More</a></div>';
    }

    } else {
    $output .= '<li>No posts found</li>';
    }

    echo $output;


    Thanks
  • I'm not sure if this will work, but it'd be my first guess. I'm only posting the foreach section since it's the only thing relevant:
    foreach ($posts as $posts) {
    $post_title = stripslashes($posts->post_title);
    $permalink = get_permalink($posts->ID);
    $thedate = get_the_date($posts->ID);

    $output .= '<div class="home-post"><h1>' . htmlspecialchars($post_title) . '</a></h1>';

    $output .='<p class="date">' . $thedate . '</p>';

    if($excerpts) {
    $output.= '<p>' . stripslashes($posts->post_excerpt) . '</p>';
    }

    $output .= '<a class="read-more right" href="' . $permalink . '">Read More</a></div>';
    }