I’m creating shortcode [latest_post] and want to show post date, title and short content. (content has “more” separator). Here is code:
function shortcode_latest_post() {
global $post, $more;
$tmp_post = $post;
$tmp_more = $more;
$posts = get_posts(array('numberposts' => 1, 'post_status' => 'publish'));
$output = "";
foreach($posts as $post) {
setup_postdata($post);
$more = 0;
$output .= '<div class="latest_post">';
$output .= '<span class="date">'. get_the_date() . '</span>';
$output .= '<h3><a href="'. get_permalink() .'">'. get_the_title() .'</a></h3>';
$output .= get_the_content("Read more...");
$output .= "</div>";
}
$post = $tmp_post;
$more = $tmp_more;
return $output;
}
add_shortcode("latest_post", "shortcode_latest_post");
But I have problem with “get_the_content” function. It returns full content text instead of short text and more link.
Can anybody help me, please?
Update:
I’m found solution. Code above is updated. – http://codex.wordpress.org/Customizing_the_Read_More#How_to_use_Read_More_in_Pages
get_the_content()
does what it’s supposed to, gets the content. You’re looking forget_the_excerpt()
. 😉Alternatively, using what you have, it should cut off wherever you put
<!-- more -->
in your post when usingget_the_content()
.