Support » Plugins » Hacks » How to change iframe source on page refresh, by post category

  • Resolved KennyWithers

    (@kennywithers)


    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 3 replies - 1 through 3 (of 3 total)
  • Thread Starter KennyWithers

    (@kennywithers)

    I also found this on the Worpress documentation:

    <?php
    $category = get_the_category();
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    }
    ?>

    So, I am going to try something like this later tonight:

    <?php
    $category = get_the_category();
    if($category[0]){
    $urls = array (
      "http://site.com/video1.mp4",
      "http://site.com/video2.mp4",
      "http://site.com/video3.mp4"
    );
    $rand = mt_rand (0, count ($urls) - 2);
    echo "<iframe src='".$urls [$rand]."'></iframe>"; }
    ?>

    I’ll create a new one for each category number and let you know if I get it working.

    Thread Starter KennyWithers

    (@kennywithers)

    I think I’m getting closer. This is what I have so far:

    <?php
     $urls = array (
       207 => ("http://example.com/page1.html","http://example.com/page2.html"),
       208 => ("http://example.com/page3.html","http://example.com/page4.html")
       209 => ("http://example.com/page5.html","http://example.com/page6.html")
       );
    
     $rand = rand(0,1);
    
     $category = if category is 207 display array 207, If category is 208 display array 208, else display array 209
    
     echo "<iframe src='".$urls[$category->Cat_ID][ $rand ]."'></iframe>";
    
    ?>
    Thread Starter KennyWithers

    (@kennywithers)

    I figured it out, and I thought I’d share the love with the community. Below is the correct code, tested and working.

    <?php
    
    /*Enter the WordPress category number, then the url's that you want to rotate in the iframe*/
     $urls = array (
        207 => array("http://example.com/page1.html","http://example.com/page2.html"),
        208 => array("http://example.com/page1.html","http://example.com/page2.html")
            );
    
      /*This gets the category*/
      $category = get_the_category();
    
      /*This counts the number of urls in the selected category*/
      $randcategory = rand(0,(count($category)-1));
    
      /*this randomizes the urls in the category*/
      $category = $category[$randcategory];
    
      /*If more than one category is selected for a post, this randomizes all urls in the categories selected*/
      $rand = rand(0,(count($urls[$category->cat_ID])-1));
    
      /*This checks to see if there are urls in a category. If so, then it displays the random urls in an iframe. If not, then nothing appears including the iframe.*/
      if(!empty($urls[$category->cat_ID][ $rand ])){
      echo "<iframe width='' src='".$urls[$category->cat_ID][ $rand ]."'></iframe>";
      }
    ?>

    There are many ways of finding out what the category number is of a post in WordPress. You can hover over the category name in the WP Dashboard, and the number will be at the end of the url. You can also download the plugin “Reveal ID’s”. Also you can enter the following php code, and enter it in the WordPress Loop of your single.php page.

    <?php
      foreach((get_the_category()) as $category) {
      echo $category->cat_ID . ' ';
      }
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to change iframe source on page refresh, by post category’ is closed to new replies.