Randomly Display Advert / Content In WordPress via Genesis Simple Hook

I previously wrote a post on how I effortlessly customize my Genesis theme Footer’s links and credit using the simple Hooks plugin. In today’s article, I will show us how to randomly display content (ads, images, text etc.) hooked into any of the Genesis WordPress hook using same plugin.

Let say we want to randomly display two advert A and B. Below is the respective Ad code for both adverts.

Advert “A”


<a href="http://advertA.com">
<img src="http://adverA.com/logo.jpg"/>
</a>

Advert “B”


<a href="http://advertB.com">
<img src="http://adverB.com/logo.jpg"/>
</a>

Below is the PHP code that will randomly display the Advert.


< ?php
// Save the ad codes in a PHP variable
$a = '<a href="http://advertA.com">
<img src="http://adverA.com/logo.jpg"/>
</a>';
$b = '<a href="http://advertB.com">
<img src="http://adverB.com/logo.jpg"/>
</a>';

// set the number of randomly display adverts
$adcount = 2;

// Create and store the ad code variable as an Array
$ads = array($a, $b);

// Randomly display the advert
echo $ads[mt_rand(0, ($adcount-1))];

Take Note: say you want to randomly display five (5) adverts, include the advert PHP variable in the $ads array and change $adcount variable value to 5.

Copy the above PHP code and insert it into the preferred Hook textarea and save.
Inserting PHP code to simple hook plugin that will randomly display adverts

Do not forget to check / tick the Execute PHP on this hook? checkbox.

Don’t miss out!
Subscribe to My Newsletter
Invalid email address