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.
This version is not compatible with Wordpress 2.3+ Please use this version.
< ?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).
- (string) Determines how the random posts are displayed. Current options are
ulfor unordered list,
pfor paragraphs [each post would be in it's own set of <p> tags], and
brfor one paragraph with a line break after each post. More options will be offered as development and suggestions continue. Default is
- (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
- (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
h2was used in the previous parameter,
h2should be used in this parameter. Default is
- (integer) Determines the number of posts to display. Default is
- (boolean) Value to determine if the posts excerpt content should be shown in a
pelement following the post title. Default is
- (integer) Will only search this category for random posts if a category is given. Default is
- (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.
- (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 0.5 — October 23, 2007
includeExcerptparameter to optionally allow showing the post's excerpt content
Version 0.4.3 — March 23, 2007
- Made various adjustments to avoid potential conflicts with other plugins
Version 0.4 — January 18, 2007
- Set internal default values so if the plugin is called with all
nullvalues it will still display results
- Only categories with at least one post will be shown
- Various code updates
Version 0.3 — March 25, 2006
- Updated compatibility for Wordpress 2.0
- Added option to specify category to pull random posts from
- Various minor code updates and code comments added
Version 0.2.5 — January 10, 2006
- Fixed bug with specifying custom fields
Version 0.2.3 — November 14, 2005
- Released to the public
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.