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.
the_time() echo's the time, use get_the_time() instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With