Random Featured Post Plugin

Download from WordPress.org

The Featured Post plugin allows you to display a random post from a designated category. Ideally this should draw readers landing on your homepage to some of your best posts. The featured post will display the title ‘Featured Post’ (customizable) along with the post’s title, an excerpt and a link to continue to the full post.

This plugin requires Wordpress version 2.3 or higher and has been tested up to version 2.5.1.

Installation

  1. Download and unzip featuredpost.zip (see below) to your plugin folder.
  2. Activate the plugin from the Plugins section of your dashboard.
  3. (Optional) Create a ‘Featured’ category.
  4. Go to the ‘Featured Post’ options in your dashboard and select the categories from which you would like posts to be randomly selected.
  5. Check the ‘Show Featured Post’ box and click ‘Update’ to save your changes.
  6. Place a call to the show_featured_post() function in your template, most likely in your main index template just before the loop. See additional notes below.

Additional Notes

  • The show_featured_post($PreFeature = ”, $PostFeature = ”, $AlwaysShow = false, $categoryID = 0) function has five optional arguments. The first two are for any HTML you would like to display before and after the featured post. The third argument is a boolean (true/false) argument which will override the ‘Show Featured Post’ option and always display the featured post when true. The fourth option allows you to specify a category to display from, overriding the selection in settings page.
  • If you choose to display excerpts, the plugin will display your optional custom excerpt from the advanced options section of your post; otherwise, the standard Wordpress excerpt is shown. The fifth option allows you to show more than one post at a time, overriding the settings page option.
  • This plugin respects the <!–more–> tag when you choose to display the full content. There is a setting which allows you to ignore the tag and display the full post.

Examples

  • This example will display the featured post only on your homepage inside a span block:

    if ( function_exists('show_featured_post') )
     if (is_home() && !is_paged())
      show_featured_post('<span class="container">','</div>');

  • This example will always display the featured post, ignoring the ‘Show Featured Post’ option and the category ID option in settings:
    if ( function_exists('show_featured_post') )
     show_featured_post('', '', true, 42);

  • All output is contained in a DIV class named “featuredpost” and can be adjusted via your stylesheet.

Change Log

v1.1.3 (May 15, 2008)

  • Bug fix: more tag was not being respected on single posts/pages when full content option was selected.

v1.1.2 (May 5, 2008)

  • Added option of showing posts and/or static pages.

v1.1.1 (April 30, 2008)

  • Added multiple category selection option.
  • Added option to display multiple posts.

v1.1.0 (April 28, 2008)

  • Discontinued use of truncated post content and switched to the option of displaying full post content with HTML intact or standard excerpts, custom or Wordpress generated.
  • Added option to ignore the <!–more–> tag when displaying full post content.
  • Added fourth argument to show_featured_post() function to allow category ID from settings to be overridden.

v1.0.1 (April 11, 2008)

  • Added category name and ID table to options page for convenience.

v1.0.0 (April 7, 2008)

  • Initial plugin release.

Bugs/Feature Requests
You can leave a comment below or contact me at scott(at)mydollarplan(dot)com.

 

If you find this plugin beneficial, please consider donating for the author’s time. Any little bit is appreciated!


Download from WordPress.org

62 Responses to Random Featured Post Plugin

  1. Great plugin and just what I’m looking for. Many thanks.

    Unfortunately, as a relative newcomer to Wordpress and adjusting css style sheets, the additional notes and examples provided are a bit beyond me. Not quite sure where to place the code. Any chance of a more detailed step-by-step on how to do this?

    Kate Foy
  2. Kate,
    I’m not very good with CSS either but here’s my best attempt at an explanation. In your Design-Theme Editor, open your stylesheet and create a section like this:
     
    #content .featuredpost {
    border:1px solid #000000;
    }
     
    You can further adjust other elements in the featured block, such as the post title, like this:
     
    #content .featuredpost h2 {
    color: #FF0000;
    }
     
    Does that help?

    Scott
  3. I’ll give it a shot. Thanks Scott.

    Kate Foy
  4. Hi Scott

    no go. This is what I have done:

    downloaded, unzipped and uploaded the plug-in to my plug-ins folder; activated the plug-in on my WP Dashboard; inserted the category name for random post to choose from; saved. BTW, I assume Category ID is the name of the category? It’s not showing after I hit the save button.

    I then inserted this code in the index.php within my theme folder:

    <?php if ( function_exists(’show_featured_post’) )
    if (is_home() && !is_paged())
    show_featured_post(”,”);?>

    And in my CSS style sheet for the theme, I’ve inserted:

    #content .featuredpost {

    border:1px solid #000000;

    in the content styles section of the file.

    I can only think I’ve got the code in the wrong place, either within the particular file or in a wrong file altogether.

    Sorry to be a pain, but I’d really appreciate a solution as I’ve gone past the ‘forget it’ stage and want to make it work! Call me ornery …

    Best

    Kate Foy
  5. Kate,
    The category ID is a number, it won’t save if you enter text. In your dashboard go to Manage-Categories and click the category you want. Then look in the address box of your browser and you’ll see a cat_ID= at the end of the URL. The number at the end is what you want. Also, did you check the “Show Featured Post” box in the options? If you need more help just shoot me an email.

    Scott
  6. Is there a way in which you can put more control in this plugin? Such as the fact, that some users may not want a random post, but posts ordered in terms of the newest first..

    Nick
  7. Nick,
    Do you mean that you want to always display the single newest post from a specific category as your featured post? Or am I reading your comment wrong?

    Scott
  8. Very interesting plugin. I’ll probably use this for some of my future theme developments.

    @Nick: You can probably just use a wp_Query to do that.

    Good luck!

    Leland
  9. Ok Im a retard…. Could anyone do a step by step guide for this?? I mean down to the \”T\”… Sorry and thanks for all the help…

    Tim
  10. This is the perfect plugin for me except I need to allow it to show images — any advice? I am trying to show a thumbnail that links to my latest project but right now I can only display text.

    Kristen
  11. @Kristen

    The plugin shows the content of a post. I can put in an option to not strip HTML tags from the post content which would show images in your post, along with any text you might have as well though.

    Scott
  12. That would be perfect. I tried examining the plugin code but couldn’t identify what exactly I needed to modify.

    Kristen
  13. @Kristen

    Look for the lines

    $desc = attribute_escape(strip_tags($post->post_excerpt));

    and

    $desc = attribute_escape(strip_tags($post->post_content));

    and replace them with

    $desc = attribute_escape($post->post_excerpt);

    and

    $desc = attribute_escape($post->post_content);

    respectively.

    Scott
  14. I modified the plugin as instructed and it nearly works — except instead of displaying the image you see <img src=”…..”

    Kristen
  15. Sorry, forgot to take off the attribute_escape functions as well. The lines should be

    $desc = $post->post_excerpt;

    and

    $desc = $post->post_content;

    Scott
  16. Thanks a million, it definitely works now!

    Kristen
  17. Great plug-in, thanks. I’m using the following classes in my stylesheet, which work well for me:

    h3.featuredpost

    h2.featuredpost a

    Andrew
  18. this actually works a bit better though after tinkering:

    div.featuredpost h2 a

    div.featuredpost h3

    Andrew
  19. Hi, thanks a lot for this great plugin. I like it very much and you can see it in action on my site.

    Some questions:

    I would like to use the more-tag including images instead of truncating the text. Is that possible or do you plan this feature?

    The solution in comment 15 breaks the formattings when attributes like strong or italic are used? Is it possible only to use the img-tag inside the truncated text?

    Best wishes and excuse my chaotic english.

    Daniel

    Daniel
  20. another question is, if it possible to use this as a widget?

    Daniel
  21. @Daniel,

    Widgetizing this plugin is not possible; however, there are other plugins out there for displaying featured posts in your sidebar if that’s what you would like to do. Check the Wordpress.org plugin database. I’ll take a look at your other feature requests when I have a chance.

    Also, if anyone is just looking for a plugin to display a single featured post and is not interested in randomizing from a group of posts then I would recommend using the WP-Sticky plugin.

    Scott
  22. Hi Scott,

    yes I wanted to show your random featured post in a sidebar, because I like the flexibility to manage a group of articles through the categories, which are important for me :-)

    It would be really great to use an eyecatcher too.

    Daniel
  23. I’m not a coder, but this worked for me:

    $desc = strip_tags(($post->post_excerpt), ”);

    Daniel
  24. Hi Scott,

    Thanks for sharing with us this plugin. It works well in our site ~ with truncated text, etc.

    Because I wanted to display the entire text of the post being rotated, I increased the character limit in the admin option which works okay except all the html tags and formats are lost.

    Is there a way I could display the entire text of the post being rotated with formatting being retained?

    Thanks heaps.
    Pam

    Pam
  25. Hi- I’ve added this plugin to my site and it works great, but how do I set it to show more than one featured post at I time? For now I’ve duplicated the code: , but more than often it shows the same featured posts in a row.

    Casey
  26. @Pam:
    I’ll really try to fit in some time to work on the plugin this week. In the meantime, comment 15 will do what you’re looking for, but keep in mind that if the post is ever truncated in the middle of a tag it will mess up your formatting.

    @Casey:
    This plugin is really designed to only show one featured post at a time but I’ll see about adding an optional parameter to the function call so that you can specify different categories for each call, that should help you out.

    Scott
  27. Ok Scott thanks! I only need to specify one category - but I’d just like to show more than one Featured Post at a time. Let me know if you find a way to make that work - thank so much!

    Casey
  28. The plugin has been updated with a few new options. Basically, the truncated post option has been discontinued in favor of displaying either full, formatted post content (with or without more tag) or post excerpts.

    @Casey:
    The best I could do for you is add an additional option to the function call to specify the category (see above for example). You’ll be able to have two featured posts at once but they will have to be from different categories. You can have them come from the same category by creating two new categories just for featured posts and then adding different posts to each of those categories, along with their original categories.

    Scott
  29. Thanks very much, Scott. You’re a legend!

    The admin option to use “excerpt” or “full text” with formatting remaining intact is just exactly what I’ve been looking for. :-)

    Pam
  30. Its possible to configure more than just one category?

    Miguel
  31. @Miguel:

    That’s one of the things on my “to do” list for this plugin.

    Scott
  32. Plugin has been updated with multiple category selection and multiple post options.

    Scott
  33. Wow, thats was fast… thanks for the wonderful plugin, I am using and will update.

    Miguel
  34. Hi - really useful plugin with one problem which means I can’t use it. I’ve just noticed that it displayed one of my ‘pages’ instead of one of my ‘posts’, even though pages don’t fall into any of the categories - is it possible to exclude ‘pages’?

    Paul
  35. @Paul:

    Good catch. The plugin has been updated with the option of displaying posts and/or static pages.

    Scott
  36. Sounds good, and I would like to try this plugin but after bouncing back and forth between this page and the Wordpress plugin page I cannot find it.

    Where is it?

    DigitalZip
  37. @DigitalZip:

    The link is at the top (I just added) and bottom of this page. It takes you to the WordPress.org Plugin Directory where you can find the download link on the right side of the page in an orange box.

    Scott
  38. Thank you Scott. It’s not very obvious on Wordpress that you should click on the orange download box. Well it isn’t to me :-)

    Got it now anyway.

    DigitalZip
  39. Hi Scott
    I was the first commenter here and just wanted to say thanks for your neat plug-in and for being such a responsive developer. It’s really appreciated especially by those of us who are ‘challenged’ by all this coding stuff! :-)
    I’m part of the comment08 challenge running right now to increase communication round the blogosphere, and thought I should say thanks to people who make my blogging life much easier. You’re on my list!

    Best wishes.

    Kate Foy
  40. Wow, that was quick! Since my comment I have been dabbling with using multiple loops within Wordpress, and found that this is also a good way to display a single random post alongside my usual homepage content. I’ll use that route in the interests of keeping plugins to a minimum, but this is certainly useful for those who don’t want to delve into advanced stuff like multiple loops.

    Paul
  41. Thanks Scott. Given the series of improvements that you have introduced to your plugin, I guess comment #24 has helped it realize its full potential, that there’s a big community out there looking for this kind of plugin. Good work! ~ Pam

    Pam
  42. Glad to see others are finding this useful.

    Spread the word!

    Scott
  43. hi. Thanks for plugin

    perfect.

    regards

    baron
  44. Hi Scott,

    I wanted to add this code into single.php to showw my featured articles in top of each post.

    On my Homepage it works fine but in single.php it always displays the full post?

    Best wishes

    Daniel

    Daniel
  45. @Daniel:

    Are you using the most recent version of the plugin? I’m unable to replicate the problem you’re having. Please check your settings and make sure you’ve selected “display excerpt” under the content options.

    Scott
  46. Hi, Scott.

    Thanks for a great plugin. I’ve got it working pretty well.

    I’d like to see the date of the featured post show up with the post and excerpt. Could you help me with the code for that?

    Thanks!

    Wardeh
  47. @Wardeh:

    I thought about including the date but decided against it because typically a “featured” post would be an older post from deep in your archives and highlighting the date might keep readers from clicking through, or at least that was my thinking.

    If you want to add the date you’ll need to modify the code. Look towards the end of the show_featured_post function and in the place of your choosing insert the line
    echo date(”F n,Y”,strtotime($thepost->post_date));

    Scott
  48. Hi Scott,

    I use 2.3.3 and K2. The problem is, that the same functions has different results. On my homepage everything works fine, but in single.php I got the whole article.

    No, I use full content with “more-tag”. If I switch to “excerpt” everything works fine.

    Daniel
  49. @Daniel:

    I hadn’t tried the full content option with the more tag on a single post. I found the bug and the plugin has been updated. Thanks for bringing it to my attention.

    Scott
  50. hi scott, thanks a lot. hope your private million dollar plan works :-)

    but there is another mysterious behaviour. on the homepage it works again fine, but now after the more tag, only on single page, I get the list of related articles inside it?

    Daniel
  51. @Daniel:

    I’m guessing this is because whatever plugin you are using to generate the related articles is hooking the function that displays the formatted content. I’m not sure how to get around this. What plugin do you use for related articles?

    Scott
  52. hi scott, I use simple tags: http://wordpress.org/extend/plugins/simple-tags

    Daniel
  53. @Daniel:

    I just glanced at the code for that plugin and I’m near certain that’s the issue. You can probably test it by disabling that plugin. I’m not sure how to get around that, but I’ll do some thinking and let you know if I figure it out.

    Scott
  54. You’re right. When I disable the plugin everything works fine, as it should.

    It’s a pitty, because I like simple-tags for managing the tags very much. Hope you find a solution.

    Daniel
  55. but for the moment I decided to disable the related posts, because your way of featuring articles is much more attractive.

    Daniel
  56. @Daniel:

    I’m glad you find the plugin so useful. Until I can find a fix, you might want to check out another plugin to do the related posts. We use Similar Posts here and I’m happy with the way it works.

    Scott
  57. I’ve successfully done steps 1-5 but 6 is where I get confused… what does it mean to place a call? and what is a loop… What exact code should I be placing in my index.php per theme??

    Linz
  58. @Linz:

    In index.php look for the line that has “while (have_posts())” in it; that’s the start of the loop. Just before that line place the code from the first example above, making sure to include it between the “” tags.

    Scott
  59. Great plugin. Anyway, can it be inserted into the page/page?

    Thx

    gofree
  60. @gofree:

    Yes, but if you read the other comments you’ll see that it may conflict with some plugins.

    Scott
  61. Hi Scott,

    I am trying to call it using this show_featured_post() with Adman plugin to have the featured post appear on the top of index in homepage, but it seems not working as it shows the code show_featured_post() back.

    Did I do sth. wrong.

    Thanks

    gofree
  62. @Scott. Great tipp. Thanks again.

    Daniel

Have a comment on this article?

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Subscribe without commenting


Trackbacks/Pingbacks [link]