WordPress latest post shortcode for WYSIWYG Editor

Ever needed to show a link to the latest post dynamically in the text editor in WordPress?

Shortcodes are great little tools and easy to create, firstly you need to load up your themes functions page and add the following code.

[sourcecode]

function my_recent_posts_shortcode(){

$args=array(
‘post_type’=>’post’,
‘category_name’=>’news’,
‘max_num_pages’=>20,
‘posts_per_page’=>1
);

$wp_query = null;
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$latest = get_permalink();
endwhile;

return $latest;
}

add_shortcode(‘recent-news’, ‘my_recent_posts_shortcode’);
[/sourcecode]

What you will see if your shrewd is the WordPress query code, the arguments to specify 1 post and in particular a category filter! Yes folks you get that bit for free! If you want to to just show the latest from a category use category_name=new. Now in your wordpress editor just drop in the tag

[source]
[recent-news]
[/source]

As they used to say in the olden days,

Viola!