WordPress Plugin: Get Recent Post

This is a plugin that satisfies a simple need in WordPress. A function that returns text from one post (the most recent) for one category. That’s it! :) Save the Get Recent Post plugin to your wordpress/wp-content/plugins directory, then remove the .txt extension. You can see it in action on my site’s front page sidebar, with the “Site News” category.

(Versions of Wordpress prior to 2.3 will need to use Get Recent Post 1.3.2.)

Update: New Release! Download version 2.0 of Get Recent Post. This new version will ONLY work with Wordpress 2.3.x, 2.5.x, 2.6.x, and 2.7; it is not compatible with earlier versions of Wordpress. Please use version 1.3.2 of this plugin for older versions of Wordpress.

Function Usage

After activating your plugin, you can place the get_recent_post() function anywhere in your php templates. I placed it in my sidebar.php template, as:

< ?php _e(get_recent_post(2,0,0,1,0,0)); ?>

(Make sure you remove the space between < and ?php in my example above… WordPress puts the space in there so PHP won’t process it.)

The syntax of the function is:

get_recent_post(post_category_id, post_display, link_to_post, paragraph_pad, title_style, trunc_more, alt_link)

For the post_category_id variable, 2 is my “News” Category ID. For the post_display variable, use the following values:

0 = Display the post title and content (* default)
1 = Display the post title and excerpt
2 = Display only the post title
3 = Display only the content
4 = Display only the excerpt

For the link_to_post variable, use the following values:

0 = Do not link the post title to the post's permalink (* default)
1 = Link the post title to the post's permalink
2 = Link the post title to the alternate link specified in alt_link

For the paragraph_pad variable, use the following values:

0 - Do not pad the content with < p > tags
1 - Pad the content with < p > tags (* default)

For the title_style variable, use the following values:

0 - Display the post as < p >< b >title< /b >< /p >post (* default)
1 - Display the post as < b >title< /b >:post

For the trunc_more variable, use the following values:

0 - Do not truncate post at < ! --more-- > tag (* default)
1 - Truncate post at < ! --more-- > tag

For the alt_link variable (after specifying 2 in link_to_post), use the following values:

'http://www.site.org'
Note that the URL must be surrounded by apostrophes (single quotes). Otherwise, you will get an error. If I wanted to link the title of the most recent post in category 2 to http://www.site.org, the function call syntax would look like this:

< ?php _e(get_recent_post(2,0,2,1,0,0,'http://www.site.org')); ?>

Function Output

The output of the get_recent_post() function will look like the following:

Title

Content

This plugin has been tested to work with WP 2.2.1. To see it in action, go to my home page and scroll towards the bottom of the sidebar, under “Site News”.

I sometimes receive questions as to how to display more than one post. One fellow WordPress user, VirusHead, offered this solution with WordPress’s wp_get_archives template tag:

I just added:

*?php wp_get_archives(’type=postbypost&limit=20′); ?*
Replace * with bracket (I hope this shows up).
This is set to 20 titles, but you can set whatever number you want.

Comments are closed.