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) {
        $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?


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 for get_the_excerpt(). ;)

Alternatively, using what you have, it should cut off wherever you put <!-- more --> in your post when using get_the_content().

  • Thanks, but that is not what I want. After some researching, I'm found solution and updated the question.
    – sasa
    Mar 29 '12 at 18:38
  • 3
    @sasa You should post your solution as an answer and then accept it as the best answer. :)
    – Jared
    Mar 29 '12 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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