Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

KennyWithers on "How to change iframe source on page refresh, by post category"

$
0
0

Hello,

I have been looking for a way to rotate iframes with ads on my single post page (single.php). I would like these to randomly rotate or change every time the page is refreshed. I searched the forum and found this very basic php code:

$urls = array (
  "http://site.com/video1.mp4",
  "http://site.com/video2.mp4",
  "http://site.com/video3.mp4"
);

$rand = mt_rand (0, count ($urls) - 1);

echo "<iframe src='".$urls [$rand]."'></iframe>";

The only problem is that I want to load unique iframes depending on the category of my site. For example, If I have a post about cars, I want car related products to rotate in my iframes. I don't know enough PHP to code it myself, but I believe what I need is something like this:

Category 1 $urls = array (
  "http://site.com/video1.mp4",
  "http://site.com/video2.mp4",
  "http://site.com/video3.mp4"
);

Category 2 $urls = array (
  "http://site.com/video1.mp4",
  "http://site.com/video2.mp4",
  "http://site.com/video3.mp4"
);

Category 3 $urls = array (
  "http://site.com/video1.mp4",
  "http://site.com/video2.mp4",
  "http://site.com/video3.mp4"
);

If post is category 1, display "Category 1 $urls"
$rand = mt_rand (0, count ($urls) - 2);

If post is category 2, display "Category 2 $urls"
$rand = mt_rand (0, count ($urls) - 2);

else display "Category 3 $urls"
$rand = mt_rand (0, count ($urls) - 2);

echo "<iframe src='" [Category} .$urls [$rand]."'></iframe>";

Does anyone have any suggestions?

Thank you for any help!


Viewing all articles
Browse latest Browse all 8245

Trending Articles