For example if you’ve stored geographic coordinates (longitude and latitude) for your posts in a custom field, you can use this plugin to randomly generate a link to Google Maps. Perhaps you could even generate a random embedded Google Map centered on that spot. I know that some people use Custom Fields to store images with special meanings. They could use this plugin to randomly display an image linking back to the post it came from, similar to my Random Image Plugin.
This is not a sexy plugin, but it just might save the day. Here’s a screenshot of the admin interface.
- Download and unzip random-custom-fields-1.0.zip (v1.0)
- Upload the file random-custom-fields.php to your plugins directory:
- Activate the plugin (don’t forget!)
- If your theme is widget-enabled, goto Design > Widgets and add “Random Custom Fields” to your sidebar
- If your theme is not widget-enabled, edit the index.php or sidebar.php template file in your theme’s directory:
and add the following line of code where you want a random custom fields to appear:
<?php random_custom_fields(); ?>
- Configure the plugin from within WordPress under Settings > Random Custom Fields
<?php random_custom_fields(); ?> function can take 9 optional parameters in between its parentheses. If you specify any parameters this way, all settings made through the configuration interface will be ignored. Note: this behavior could be useful if you wanted to have several different random custom fields each with different settings appearing throughout your site.
<?php random_custom_fields(custom_field_name, include_posts, include_pages, sort_randomly, count, value_separator, html_template, html_between_templates, category_filter); ?>
- custom_field_name is a required string that determines which custom field name to display.
- include_posts is a boolean (default is true) that determines whether to pull custom fields from posts.
- include_pages is a boolean (default is false) that determines whether to pull custom fields from pages.
- sort_randomly is a boolean (default is true) that determines whether to pull custom fields from posts in random order, or in date descending order.
- count is an integer (default is 1) that determines how many custom fields to display.
- value_separator is a string (default is newline) that allows you to break up the custom field value into multiple pieces for use in the html_template.
- html_template is a string (default is
%0) that determines how the plugin should display the custom field. There are several variables that the plugin can output by default, including
%excerpt, all from post of the custom field. The variable
%0will be replaced with the entire custom field value. If you’ve entered a custom value_separator (default is a newline) the variables
%3, etc will be replaced with each piece of the custom field value divided by the value_separator delimiter.
- html_between_templates is a string (default is
<br /><br />) that gets printed between custom fields if the count parameter is greater than 1.
- category_filter is a comma-separated list of category or tag ids (default is blank) that limits the custom fields posts within certain categories or tags.
Questions, comments, and suggestions are always welcome. If you’re interested in contributing to the code behind Random Custom Fields, it’s hosted on GitHub.