There are times when you’d like to have a certain type of post appear in a widget for use in your sidebar or (in some themes) your header or footer. The Random One Cat Widget allows you to do this. It will display any post, long or short, even ones containing pictures and custom fields, although it is best suited to small text posts.
On this site I use it to display a category of posts I called tips in the header. These are normally just one liners that I sometimes add to the site. On my poetry site (The Fork of Ambiguity), I use this to display a random poem on each page, but it will work equally well for any group of reasonably small posts.
Download
You can download this Widget from the WordPress Plugin Directory.
Please drop me a line if you use this plugin; I’d be interested to see how it is being implemented.
Features
There is the option to include the title of the post, the title of the widget, both or (like on this site) neither. The later offers a cleaner approach, but you choice is there for you should you prefer a header or two.
If you want you can add a bunch of widgets, each showing a random post from a different category.
There is also the choice to show any custom fields that you might have defined for the post. Just check the box.
You can now display up to five posts at a time.
Technical
- Compatibility: 2.8+
- xHTML, php5.2 and MySQL 5.0 (strict mode) compliant
- CSS integration
Notes
The display uses classes to define the format of the field title and names, so you can add (or edit) the following classes in your theme style-sheet to suit:-
Header : .post-meta
Value: .post-meta-key
For example:-
.post-meta {font-variant: small-caps; color: maroon; padding-top: 10px; }
.post-meta-key {color: green; font-weight: bold; font-size: 110%; }
Version History
2.2
- Added an unordered list for title only displays
2.1
- Fixed custom field bug
- Fixed empty value issue
- Added option to display only title links
- Tidied widget parameter selection
- Multi-lingual ready (translations required)
2.0
- Added Changelog
- Added option to display up to five posts
- Fixed empty post-meta ul display bug
- Moved to WP_Widget class
1.3
- Added selection for widget title. Show the widget title, the post title, both titles or neither.
1.2
- Updated for 2.9
- Processes short codes (useful for image captions and gallery posts).
1.1.2
- Minor updates to code layout for 2.8
1.1.1
- Minor updates to code layout for 2.7
1.1
- Updated for 2.6
- Fixed random selection bug in this version
1.0
- First Production Release
0.4
- Added option for custom field display (requested by Herve)
0.3
- Updated for 2.5.1 compatibility
- Update version and release information only
0.2
- Complete overhaul for 2.5 compatibility.
0.1
- Initial Release
Hi, your tool is great but i need to use the title of post as Header and i want to use an image for post (with custom fields) so i’ve made an improvement for that purpose.
If you want, let me send it to you..
Finally, thank you for that great plugin again…
Thanks for the feedback. The widget already has the option to include the title of the post, display images and add custom fields so there should be no reason for you to update the code for these things.
Is there a php-type function I can call instead of using the widget.
I want to put this into a page template (so a random quote appears at bottom of all pages) without setting up a widget area. Just a call to a php function.
Can that be done?
It can be done using some of the widgets code, but not directly by calling the widget via a function because the widget requires a specific environment which isn’t available on a normal page (unless you code it in yourself, but if you do that you might as well make a widget area). The code for a stand alone function would be relatively simple to do though if you were prepared to hard code values.
Thanks for the great plugin. The single “Testimonial” is a perfect use for this. Now, in the inner-right sidebar on the homepage there I’m trying to use this to pull content only from what is essentially a link list, and it works, but layout is very different to other list widgets, and while you mention in the notes above what to work with in the CSS to change the title and name (post-meta and post-meta-key), you don’t mention the content. Can you tell me how to make changes to that? If I can add to the CSS for this, that would great, but I haven’t yet been able to figure out what it’s referencing for the content, if anything. Thanks very much for your help!
Thanks for the feedback. Each widget is wrapped in in a div which WP have helpfully given both an id and a class. You can use that to reformat any contents (the class name is widget_random_one_cat). The content is displayed “as is” i.e. no additional formatting or html is applied to it by the widget, so it won’t give you a formatted list unless this is already in the content. It maybe that I’ve not understood – if you’d like to go into it more please drop me an email.
That’s what I expected, but was unsure. I’ll see if I can work out a good method to deal with my formatting issue from here. Thanks very much for the quick reply!