Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Wowordpress, I needed the exact same thing; a way to display rotating iframes at the bottom of a post and have them change on page refresh. But, I also wanted different ads to show depending on the post category. I had a friend help me with the php code. I will add it below:

    <?php
    
    /*Enter the WordPress category number, then the url's that you want to rotate in the iframe*/
     $urls = array (
        207 => array("https://example.com/page1.html","https://example.com/page2.html"),
        208 => array("https://example.com/page1.html","https://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 . ' ';
      }
    ?>

    You can then use this plugin to place the ads where ever you want on your site. Make sure you check the “Process PHP” checkbox. If you want to insert the ads somewhere other than the default locations in the plugin, you can enable “PHP function adinserter for block 1” then add the code <?php echo adinserter (1); ?> anywhere in your pages using the editor.

    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("https://example.com/page1.html","https://example.com/page2.html"),
        208 => array("https://example.com/page1.html","https://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 . ' ';
      }
    ?>
    Thread Starter KennyWithers

    (@kennywithers)

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

    <?php
     $urls = array (
       207 => ("https://example.com/page1.html","https://example.com/page2.html"),
       208 => ("https://example.com/page3.html","https://example.com/page4.html")
       209 => ("https://example.com/page5.html","https://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 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 (
      "https://site.com/video1.mp4",
      "https://site.com/video2.mp4",
      "https://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.

Viewing 4 replies - 1 through 4 (of 4 total)