Screenflicker

home

Wordpress plugin: Random categories with random posts

14 Nov 2005

I've finally gone and done it, I wrote my first Wordpress plugin. Due to time constraints, lack of sleep, and wanting to be as descriptive as possible I've decided to call it Random categories with random posts, catchy isn't it.

What prompted this fit of initiative? A simple request. Peter Flaschner from The Blog Studio needed some code that would hook into WP and display a random category. Simple enough, there are plugins that do that. The difference was that he needed to grab a number of random posts from that random category — which linked to their permanent place in the blog as well. Two days later I was finished.

Download Version 0.5

This version is not compatible with Wordpress 2.3+ Please use this version.

The details

Usage

< ?php echo randomcategoryandposts('displaytype', 'elementbeforeheading', 'elementafterheading', numberofposts, includeExcerpt, 'categoryid', 'customfieldname', 'customfield_value'); ?>

-or if you would like to store it in a variable for later use-

< ?php $randPosts = randomcategoryandposts('displaytype', 'elementbeforeheading', 'elementafterheading', numberofposts, includeExcerpt, 'categoryid', 'customfieldname', 'customfield_value'); ?>

Note: When placing the code on your site, remember to remove the space between the '<' and the question mark (?) from the above examples. Also, be sure to populate the code with the correct values (see below).

Parameters

display_type
(string) Determines how the random posts are displayed. Current options are ul for unordered list, p for paragraphs [each post would be in it's own set of <p> tags], and br for one paragraph with a line break after each post. More options will be offered as development and suggestions continue. Default is ul.
element_before_heading
(string) Determines what HTML tag should be used to style the random category name. This value can include CSS classes provided the quotation marks are escaped in the function call [eg., random_category_and_posts("ul","h2 class=\"foo\","h2"); ]. Default is h2.
element_after_heading
(string) Similar to the previous parameter, this is the value that should be in the closing HTML tag for the random category name. For example, if h2 was used in the previous parameter, h2 should be used in this parameter. Default is h2.
number_of_posts
(integer) Determines the number of posts to display. Default is 5.
includeExcerpt
(boolean) Value to determine if the posts excerpt content should be shown in a p element following the post title. Default is FALSE.
category_id
(integer) Will only search this category for random posts if a category is given. Default is NULL.
custom_field_name
(string) Specifies the name of the custom field the plugin should check for in the random posts. If not specified any post in the category choosen can be displayed. Default is blank.
custom_field_value
(string) Specifies the value of the custom field choosen in the previous parameter. For instance, if the custom field in the previous parameter was called "foo" and this parameter was set as "bar" only posts that contained the value "bar" in the "foo" field would be shown in the random list of posts. Default is blank

Version history

Version 0.5 — October 23, 2007

Version 0.4.3 — March 23, 2007

Version 0.4 — January 18, 2007

Version 0.3 — March 25, 2006

Version 0.2.5 — January 10, 2006

Version 0.2.3 — November 14, 2005

Installation instructions

Installation is fairly straightforward, simply expand the downloaded file random-cat-with-rand-posts.php into the /wp-content/plugins/ directory. Load up your WP admin page in your browser of choice and activate the plugin.

Any suggestions or praise will be happily accepted in the comments of this post. If you have a question or bug report send me an email and we'll get it sorted out.

comments powered by Disqus