WordPress Random Related Posts Based on Category

Follow @tristarweb

10.1.10 – Plugin has been updated to allow you to choose the order of posts if not displaying randomly.

The other day I was in desperate need of a good, random, related posts plugin. In the end, I couldn’t find exactly what I needed so I thought I would just make it myself!

Download

Description

This plugin is a very lightweight method of including random related posts anywhere on your signle.php template. It has a variety of options available, including:

  • Choose the number of Posts to show
  • Choose whether it displays these posts randomly, or in order of newest post.
  • Choose whether to display a title, what tags to use, and what text should be displayed (e.g. H3)
  • Choose a class for the main ul

Installation

  1. Upload the `random_related_posts_by_cat` directory to the `/wp-content/plugins/` directory.
  2. Activate the plugin through the ‘Plugins’ menu in WordPress.
  3. Place `<?php relatedPosts(); ?>` in your templates – please see the description for more options.

More Options

To edit the options available, use the following codes:

<?php relatedPosts(
        $ppp = 4,
        $rand = true,
        $rPinctitle = true,
        $rPtitletag = 'h3',
        $rPtitle = 'Related Posts',
        $rPulclass = 'related_posts',
        $orderby = 'title',
        $order = 'ASC'
); ?>

Where:

  • $ppp = Number of posts to display (Any integer allowed)
  • $rand = sets the display to random (true/false)
  • $rPinctitle = Displays the title (true/false)
  • $rPtitletag = The type of tag to wrap the title in (Default h3, but it can be any valid HTML tag)
  • $rPtitle = The title to be displayed (This can be any text value)
  • $rPulclass = This sets the class of the ul (This can be any text value)
  • $rPexcerpt = Allows you to choose whether to display the excerpt (true/false)
  • $orderby = This allows you to order the posts by certain parameters (title/author/date/modified/menu_order/parent/ID/none/comment_count) If you want to choose the order of your posts, be sure to set $rand to false
  • $order = This can either be Ascending or Descending (ASC/DESC)

Extra Notes

I will continue to develop this plugin if I need to, so feel free to make any requests in the comments!

Download

About James

Hello there, I'm James. I studied Multimedia Design at university. I'm a designer at heart, though I love developing innovative and exciting solutions to web based products.I now specialise in front end web design and bespoke Wordpress powered websites.

9 Responses to WordPress Random Related Posts Based on Category

  1. Shahid says:

    08:02 22/12/2010

    I found this plugin useful for displaying related posts but I suggest to provide only title of related posts, please can you guide me.

  2. James says:

    08:55 22/12/2010

    Hi Shahid, I have actually updated this plugin yesterday to allow this functionality – I have just uploaded it to the site. If you re-download it and install, you can then set $rPexcerpt to true/false. Alternatively, keep track of the plugin updates here – http://wordpress.org/extend/plugins/random-related-posts-based-on-category/.

    I hope this helps!

  3. Simon says:

    17:04 29/12/2010

    Very good!

  4. Aberdeen Painters says:

    10:16 10/01/2011

    Hi, nice work,is there anyway of placing posts in preferred order.
    I use wordpress alot.I am working on my own site and will be adding a blog.
    Any pointers welcome and,again appreciate your work with the plug in.

  5. James says:

    11:06 10/01/2011

    Hi there, great idea! I have now updated the plugin to allow you to choose the order that the posts are displayed if you have $rand set to false. Take a look at the updated blog post (this page still), I have added two extra options with usage instructions. Let me know if you get on OK with it.

    Also, would anyone like me to add a post thumbnail feature to this?

    Thanks,
    James

  6. openredes says:

    14:34 08/02/2011

    I have these errors on XHTML validation:

    document type does not allow element “p” here; missing one of “button”, “map”, “object”, “ins”, “del”, “noscript” start-tag

  7. James says:

    14:53 08/02/2011

    Hi openredes,

    You are absolutely right – I will amend the current version of this plugin tomorrow to remove that. To remove it yourself, simply browse to wp-content/plugins/random-related-posts/random-related-posts.php

    Change this line:

    < ?php the_excerpt(); ?>

    to:

    < ?php echo get_the_excerpt(); ?>

    But be sure to remove the space between < and ?

  8. openredes says:

    14:21 09/02/2011

    Thanks so much James, all is working now!

  9. Peter says:

    19:11 01/05/2011

    Hi James,
    Thank you for your plugin.
    I use your plugin to randomise my posts for my new website and found it works very well.
    Is there a way that it could randomise the posts only once (say per day) and not every time I navigate to another page of posts.
    The reason is that when I move to another page of posts, the whole post order is changed every time which means that it is harder to actually find certain posts that someone may be looking for. Hope that make sense.
    I need to randomise my posts only once per day.

    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter