Advertisement
lberelson

Site 1 - rotate pic function v1

Jul 25th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Functions file
  4.  * Includes all necesary files
  5.  *
  6.  * @package custom
  7.  */
  8.  
  9. function register_my_menus() {
  10.   register_nav_menus(
  11.     array(
  12.       'primary-menu' => __( 'Primary Menu' ),
  13.       'secondary-menu' => __( 'Secondary Menu' )
  14.     )
  15.   );
  16. }
  17. add_action( 'init', 'register_my_menus' );
  18.  
  19.  
  20. function custom_widgets_init() {
  21.     register_sidebar(
  22.         array(
  23.             'name' => 'Rotate Pics Sidebar',
  24.             'id' => 'rotate_pics',
  25.             'before_widget' => '<div>',
  26.             'after_widget' => '</div>',
  27.             'before_title' => '<h2 class="rounded">',
  28.             'after_title' => '</h2>',
  29.         )
  30.     );
  31.    
  32.     register_sidebar(
  33.         array(
  34.             'name' => 'Articles Sidebar',
  35.             'id' => 'articles',
  36.             'before_widget' => '<div>',
  37.             'after_widget' => '</div>',
  38.             'before_title' => '<h2 class="rounded">',
  39.         'after_title' => '</h2>',
  40.         )
  41.     );
  42. }
  43.  
  44. // this function scans thru the directory received and loads the image file
  45. // names into an array; Then returns a randome name to the caller
  46. // If no directory is passed it will use the current dir
  47.  
  48. function  GetImageName($DirName)
  49.     {
  50.      $Img = array();  // define array
  51.    
  52.      // jpg
  53.      foreach (glob("$DirName/*.jpg") as $filename)
  54.      {
  55.       if (is_readable($filename))
  56.         $Img[]  = $filename;  // found image file
  57.      }
  58.    
  59.      // png
  60.      foreach (glob("$DirName/*.png") as $filename)
  61.      {
  62.       if (is_readable($filename))
  63.         $Img[]  = $filename;  // found image file
  64.      }
  65.    
  66.      // gif
  67.      foreach (glob("$DirName/*.gif") as $filename)
  68.      {
  69.       if (is_readable($filename))
  70.         $Img[]  = $filename;  // found image file
  71.      }
  72.    
  73.          $Imgcount = sizeof($Img) - 1; // the index is zero-based
  74.          if ($Imgcount <= 0)
  75.            return "put here the name of one of the images as default, ex: pic1.jpg";
  76.          else
  77.          {
  78.           $rand_image_ix = rand(0,$Imgcount);
  79.           return $Img[$rand_image_ix];  // return name of image
  80.          }
  81. }
  82.    
  83.    
  84.     // function to print the random image
  85.    
  86. function ShowRandomImg($size_x=300, $size_y=228)
  87.     {
  88.      $DirNm = "/wp-content/themes/custom/img/rotatepics";  // current directory
  89.      if (is_front_page() || is_page('contact') )
  90.      {
  91.       $DirNm = $DirNm  . "/contact"
  92.       $ImgNm = GetImageName($DirNm);  // retrieve random image from directory "contact"
  93.      }
  94.      elseif (is_page('feature')
  95.      {
  96.       $DirNm = $DirNm  . "/feature"
  97.       $ImgNm = GetImageName($DirNm);  // retrieve random image from directory "feature"
  98.      }
  99.      elseif (is_page('services')
  100.      {
  101.       $DirNm = $DirNm  . "/services"
  102.       $ImgNm = GetImageName($DirNm);  // retrieve random image from directory
  103.     "services"
  104.      }
  105.      else
  106.      {
  107.       $ImgNm = GetImageName($DirNm);  // retrieve random image from current directory
  108.      }
  109.    
  110.      // print image (or if preferred change to return
  111.    
  112.       echo "<img src=\"$DirNm.$ImgNm\" width=\"$size_x\" height=\"$size_y\"
  113.     alt=\"$ImgNm\" ";
  114.      
  115.      // return $DirNm.$ImgNm;  // use this to simply return the image path
  116.    
  117. }
  118.  
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement