Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does .the_time() appear before content

In the following shortcode, get_the_title() displays between the * and # characters, as expected. But the_time() always displays at the top of the page and not inside the content. Why does the_time() do that?

Is there a way to make the post time appear inside the content?

function get_recent_posts3() {
    $args = array('cat' => 8, 'posts_per_page' => '2');
    $the_query = new WP_Query( $args );
    $postcontent = '';
    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        $timestamp = '**<br>'.get_the_title().'##'.the_time('F j, Y').'<br>**';
        $postcontent = $postcontent.$timestamp;
    endwhile;
    return $postcontent;
    wp_reset_postdata();
}
add_shortcode('getrecentposts3', 'get_recent_posts3');

Output:

March 12, 2013March 3, 2013 **
Second Post Title##
****
First Post Title##
** 

Replace 'cat' => 8 with a valid category id to run the code.

like image 965
4thSpace Avatar asked Dec 01 '25 12:12

4thSpace


1 Answers

the_time() echo's the time, use get_the_time() instead.

like image 91
Connor Gurney Avatar answered Dec 04 '25 09:12

Connor Gurney