Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
Thanks, but that doesn't want to work.... –  songdogtech 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
    
Thanks! Works fine now. –  songdogtech May 25 '12 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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