Recent Posts: WordPress Plugin

Update

13th January, 2007

Recent Posts Version 2.0.0 beta is now available. Largely rewritten, it includes some new options and many more possible styles of display. Version 2 now has its own page where future developments will be documented.

Comments are now closed for this post but can be added to the new page.

Version 1.04 just fixes two bugs. The Posts plugin were not working if more than one was installed! Also some users were getting odd characters appearing where they shouldn’t.
Version 1.03 allows some parameters (like ‘before_title’) to be blank, or to be more complex, e.g., 'before_title=

. Also allows trimming an excerpt so it ends with a word or a sentence and not in mid-word. NB excerpt_length is now counted in characters and not words as previously.
Version 1.02 fixes bugs with the option to show static pages.
Version 1.01 fixes bugs with default settings when Similar Posts is not also installed.
Adds the skip parameter to ignore a number of posts at the head of the list.

 

Description

Simply displays a list of your blog’s most recent posts. Good for getting visitors to explore your site.

This plugin was designed as a cousin to Similar Posts. It will work fine on its own but if Similar Posts is installed Recent Posts will take its settings from the Similar Posts options page.

Instructions

  1. Download the latest version of Recent Posts.
  2. Upload the whole plugin folder (Recent_Posts) to your /wp-content/plugins/ directory.
  3. Go to your Admin|Plugins page and activate Recent Posts.
  4. Put 

    at the place in your WP loop where you want the list of recent posts to appear. By default the plugin wraps each post with

  5. and
  6.  

    but that can be changed.

  7. The behaviour of the plugin can be adjusted in two ways. If Similar Posts is installed you can use its options page. Otherwise, a query-style parameter does the trick (see below).

Acknowledgements

Recent Posts was inspired by Scott Reilly’s excellent Customizable Post Listings plugin.

Under the Hood

The way the list of recent posts is displayed can be set from the Options|Similar Posts page (if it is installed). You can exclude certain categories of post, for example, or change the code that comes before and after the link.

These general options can be overridden in specific cases by passing a query-style parameter, e.g.:

<!--?php recent_posts('limit=10'); ?-->
lists the 10 most recent posts
<!--?php recent_posts('none_text=sorry&#038;show_static=false'); ?-->
lists the default number of posts, excluding static pages, and specifies what to display if there are none

If you do not specify an option its value is taken from the options page.
This means you can use the template tag in different ways in different places.

The full list of parameters is as follows (with the default value in parentheses):

limit
maximum number of posts to show (5)
skip
how many posts to skip before listing (0)
show_static
include static pages (false)
show_private
include password-protected posts (false)
excluded_cats
comma separated list of categories to exclude (by ID) (9999, the default means none)
excluded_authors
comma separated list of authors to exclude (by ID) (9999, the default means none)
none_text
what to show if no posts match–can be plain text or a permalink
before_title
what to show before a link ()
after_title
what to show after a link( 

)

trim_before
remove the first instance of ‘before_title’ (false)
show_excerpt
include a snippet of the post after the link (false)
excerpt_length
how long an excerpt should be (50 characters)
excerpt_format
‘char’, the default, does nothing, ‘word’ trims the excerpt to the last full word, and ‘sent’ to the full sentence. If the excerpt would be trimmed to nothing no trimming is applied.
ellipsis
add ‘ …’ after the excerpt
before_excerpt
what to show before an excerpt ()
after_excerpt
what to show after an excerpt ( 

)

Feedback

If you try this plugin leave a comment here to let me know how you get on.

26 replies on “Recent Posts: WordPress Plugin”

  1. Looks like an interesting plugin I will try out. One quick question right away, Would it be possible to modify the recent posts to only show posts older than X. This way it can prevent redundancy of showing the same article links on the frontpage of very recent articles.

    Cheers.

  2. Hi Rob, thanks for getting back.

    I think the “skipping X posts” would be the most useful, at least for me.

    I have 3 posts on the homepage plus some links, using another plugin to show recent posts per category in the sidebar.

    Setting your plugin to show a list at the bottom of the page and skipping all of these by defining x=11 for example, would be great.

    I did a quick test run with your plugin, placed the but afterreloading the page unfortunately got a message of having a db error. I’ll post the error here but just delete it from the comment if you feel it clutters the comments too much.

    thanks.

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘) && post_author NOT IN ( ) ) ORDER BY post_date DESC LIMIT’ at line 1]
    SELECT DISTINCT ID, post_title, post_content FROM wp_posts LEFT JOIN wp_post2cat ON ID = post_id WHERE post_date

  3. Gemme: I’m glad that’s the kind of skip X you are looking for because it is easy to implement!

    As for the error message … have you specified excluded_authors=”? If you have, for the moment, specify ‘9999’ instead–it should be a non-existent category–and I’ll make the fix in code. Let me know if the problem is not that.

  4. Rob,

    I haven’t touched the code of the plugin itself so that is probably not the problem

    Could it be I place the code it in the wrong spot in the index.php ?

    Thanks again:)

  5. Gemme: Thanks for bringing this to my attention. It was a problem setting default values when Similar Posts isn’t also installed. I hope I’ve fixed it now. Let me know.

    I’ve also added the ability to skip the top X posts as you asked.

  6. Nice update Rob. It works now pretty perfect, especially the ‘Skip’.

    Thanks. I just have to figure out how to style the links + excerpts a bit better as they are all bullitpointed. Something for next week.

    Cheers,

    G.

  7. Thanks for the plugin. Any idea on how to modify it so that an excerpt won’t end in mid-sentence? Basically it should take the amount of words that you want it to and then go backwards till it finds a period and strip all characters after the period. Is there easy php magic to do this?

  8. I’m testing the plugin, and have the posts with their excerpts displaying just fine, but the pages won’t display. I’m using WordPress 2.0.5 without the Similar Posts plugin. Here’s the code I’m using:

    *?php recent_posts(‘none_text=nothing found&show_static=true&show_excerpt=true&excerpt_length=25&before_excerpt= – ‘) ?*

    (The * are replaced with brackets.)

    What do I need to change to get the pages to be included?

  9. Star: I’ve just posted an updated version (1.02) that addresses the problem. Give it a try and let me know if it works for you.

    Thanks for reporting the bug.

  10. I found this plugin useful for filtering my list of recent posts by category. One suggestion: it would be nice to have a way of specifying ‘nothing’ in the before_title, after_title options. If there is already a way to do this I may have missed it, but I got around it by setting the attribute to ‘-‘ then hacking the php to recognise this as a null character.

  11. This is a great plugin. However I am using a photopress plugin which puts in tags like [photopress:alignment_1_2_blahblah] and this code shows up in the excerpts. I don’t want the photo to show up on the front page where I am using this plugin, but I don’t want this code either. Is there anyway to just strip out everything in brackets?

    thanks!!

    ~shiva

  12. Hoboken: I’m working on a new version that will give much greater flexibility of what to display and how to display it. It involves some biggish changes so it might be a few days before I’m happy it is working properly.

    Shiva: I’ll think about your request… obviously it would be no good to strip everything in square brackets but I may he able to include an option.

  13. Thanks Rob for your work on this. Much appreciated! An option would be great. If the plugin doesn’t render such things in brackets I am wondering why it would be needed.

  14. I have a similar problem with the brackets, only it is with inserting videos. The tag looks like this: [youtube]Ge0kYHjXHL0[/youtube], so in this case one would want to get rid of not only the brackets but everything in between them. Sounds like it is getting complex!

    Also, does this plugin have to share options with similar posts? I use both and want different display options for each.

  15. Shiva & David: You are right that it is getting complicated. I personally don’t use the excerpt functionality so I haven’t thought a lot about how best it should work. I took over from the Related Posts plugin the way it displays a simple snippet with HTML tags removed. My sense is that the excerpt is just to give a glimpse of the content rather than to render it fully … but am I wrong? There are some choices then: do we render the excerpt fully, tags and all; do we try and give options to strip out whatever kind of tags the user happens to use; or do we leave it alone? Any ideas?

    David: Sharing options? I built Similar Posts first and then realised I could extend it to the other plugins easily. It seemed silly at the time to duplicate all the option page code for each plugin. I thought users would probably have a basic set of options that would be shared among the various plugins and still be able to customise each one by specifying its parameters.

    I am wondering now whether or not to give each plugin its option page. The price would be duplication of code and of option screen real estate. The pay off would be ease of use.

    What do you (all) think?

  16. I think the advantages of having seperate options pages outweights the disadvantages. It’s only one more tab on the options page and duplication of code is no big deal.

    Having the option of stripping out things in brackets would be ideal as so many other plugins use these. Something like the youtube tags mentioned above where it uses 2 sets of brackets might be too difficult?

    ~shiva

  17. Shiva: I’m coming round to the idea of separate options pages. Lots of other things are underway too. I’ve found a way of stripping any kind of pseudo-tag. I could also give the possibility of letting the tags ‘do their stuff’ just as they would in the post itself. I’m also getting somewhere with a very flexible way of specifying whatever kind of output the user wants.

    My main problems now are implementing all that without too many bugs and working out how best to specify the burgeoning set of options!

Comments are closed.