0

Why does the output of this function

// One Recent Post
function most_recent_post_shortcode() {
    return wp_get_archives( 'type=postbypost&limit=1&format=custom');
}
add_shortcode( 'recent-post', 'most_recent_post_shortcode' );

and shortcode [recent-post] show the output at the top of a post rather than in the body of the post where the shortcode is?

Is there a better way to use a shortcode and wp_get_archives (or other function) to display the title and link of a recent post?

It seems to have something to do with this question: The result of a shortcode appear BEFORE page content

3

The default of the echo argument is true for wp_get_archives(). I think this would fix it:

// One Recent Post
function most_recent_post_shortcode() {
    return wp_get_archives( 'type=postbypost&limit=1&format=custom&echo=0');
}
add_shortcode( 'recent-post', 'most_recent_post_shortcode' );

UPDATE: Previously: echo=false Now: echo=0

2
  • Thanks, but that doesn't want to work.... May 22 '12 at 22:12
  • 1
    Someone didn't read the Codex, and that person is me. It is an echo/return issue, I just didn't give echo the right value. Check the update.
    – mrwweb
    May 22 '12 at 22:47

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.