Including areas of random content in WordPress themes

A rotate script is one of the simplest ways to include files from specified folders randomly. Whether the random content you want to display are images or blocks of html code, just drop the rotate.php file into the folder you want to randomly draw from and then call the rotate.php file where you want the random content to appear.

There are many variations, but this will do the trick. Copy the following code and save it as rotate.php:

<?php $files = glob(’{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}’, GLOB_BRACE);
readfile($files[array_rand($files)]); ?>

It’s a good idea to check the rotate.php code to make sure it’s set to randomize the file type you are working with. In the code above, add the file extensions that you want to include in the randomization.

For example, if you’re using rotate.php to display random images you would call the random images like this:

<img src="http://your-site.com/random-image-folder/rotate.php" />

The Random File Plugin

This plugin retrieves the name of a randomly chosen file in a specified directory and can be used to display random images or files in your WordPress theme.

  1. Download the Random File plugin and upload to your /wp-content/plugins/ directory.
  2. Activate the plugin from the plugins page of your wp-admin.
  3. Call to the function from your template (see example below)

<img alt="logo" class="logo" src="<?php echo c2c_random_file('/wp-content/images/logos/'); ?>" />

This example shows how you could use this plugin to display a random image from the ‘../wp-content/images/logos’ directory.

For more information about the functions and possible uses of this random content plugin, visit Random File plugin.

Include content between posts

This is for index pages like index.php, category.php, archive.php and search.php, –pages where more than one post is listed. If you want to include some kind of content after every post or every nth post check out CG-InBetween.

  1. download the CG-InBetween plugin and upload the plugin to your /wp-content/plugins/ directory.
  2. Activate the plugin from the plugins page of your wp-admin.
  3. Insert the following code into your theme in your index.php (or any of the indexing WordPress templates) where you want the content to appear.

<?php inbetweenPosts(); ?>

As long as the code is placed within the loop it will work. (within the loop means after the ‘if have posts…’ stuff and before the ‘endif;’ line. The best place is usually right before ‘endwhile;’)

Configuring the in-between content plugin

Once you’ve activated the plugin, there will be a new admin panel tab called ‘InBetween’, where you can set up new ‘InBetweens’:

insert content between posts

For ‘Between’ you can enter the number of posts after which your content will be displayed. You can also use the values ‘all’, ‘last’ or ‘single’. If you want to display 2 posts, content, 2 posts, content, you would create 2 separate ‘InBetweens’. One having a Between value of 2 and the other a value of 4.

Then choose between ‘Echo’, ‘Function’ and ‘File’ for the content that will be inserted. If your content is an add, simply past your ad code in the ‘Echo’ field. If you want to execute a PHP function you would use the ‘Function’ field. If you want to insert an image or entire html file, enter the file’s location in the ‘File’ field. For this option, be sure the file is located in the wp-content directory of your site.

You can leave a response, or pingback from your own site by linking to this article.

Leave a Reply




Erin Bruce
Cassie Bruce