Get a random image to show the same on two image sections

by 5 replies
6
Ok So I have a slider on my site and then under that there are 3 boxes. If a post has a featured image it will then show that picture on the slider and in the box if its the first post. Well when there is not post it shows a random picture from 8 different pictures. The problem I have is that the random picture that correlates to the first post is not the same in the slider and the box. Below is the code. How would I fix this?

Code:
if (have_posts()) : while (have_posts()) : the_post();

		if($img_position == 'boxgrid'){
			$thumb = get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
			$pattern= "/(?<=src=['|\"])[^'|\"]*?(?=['|\"])/i";
			preg_match($pattern, $thumb, $thePath); 
         $random_number = mt_rand(1, 8);
			if(!isset($thePath[0])){
			$thePath[0] = get_template_directory_uri().'/images/slideshow/noftrdimg-'.$random_number.'.jpg';
			}
			$tmp .= '<div class="boxgrid captionfull" style="background: transparent url('.$thePath[0].') repeat scroll 0 0; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; " title="'. get_the_title().'">';
			$tmp .= '<div class="cover boxcaption">';
			$tmp .= '<h3 style="padding-left:8px;"><a href="'. get_permalink().'" title="'. get_the_title().'">'. get_the_title().'</a></h3>';
			$tmp .= '<p>'.substr(get_the_excerpt(), 0, 100).'</p>';
			$tmp .= '</div>';		
			$tmp .= '</div>';
So that is the little boxes then here is for the slider. They are both in the same php file


Code:
while (have_posts()) : the_post();
		
			$url = get_permalink();
			$theme_fields = get_post_custom_values('my_url');
			if(isset($theme_fields[0])){
			 	$url = $theme_fields[0];
			}
			   
			$tmp .='<div id="fragment-'.$id.'-'.$i.'" class="ui-tabs-panel">'. chr(13);
		      $random_number = mt_rand(1, 8);
		    if($width != '' || $height != ''){
		    	if (get_the_post_thumbnail( $post->ID, array($width,$height),""  ) == '') { $ftrdimg = '<img src="'.get_template_directory_uri().'/images/slideshow/noftrdimg-'.$random_number.'.jpg" />'; } else { $ftrdimg = get_the_post_thumbnail( $post->ID, array($width,$height),"class={$reflect}" ); }
		    } else {
		    	if (get_the_post_thumbnail( $post->ID, array(756,250),""  ) == '') { $ftrdimg = '<img src="'.get_template_directory_uri().'/images/slideshow/noftrdimg.jpg"/>'; } else { $ftrdimg = get_the_post_thumbnail( $post->ID, array(756,250),"class={$reflect}"  ); }
		    }
		    
			$tmp .='	<a class="reflect" href="'.$url.'">'.$ftrdimg.'</a>'. chr(13);
Both you can see are using random number but they don't sync. So one will have picture 3 and the other picture 5, but I want them to both have say picture 6 for the each instance.
Thanks!
#programming #image #random #sections #show
  • Are you new to programming?
    Save the random numbers generated in an array and reuse it in the second part.
  • Yes quite new. Ok I will look into how to do that. Thanks!
  • I haven't found anything about how to do this online. Do you have anywhere you could point me?
  • $reuse = array();

    ....
    $reuse[] = $randnr;

    or you can use associative arrays.. see php.net for more explination
    • [ 1 ] Thanks
  • I think I understand how this works. Basically the random number is put into an array. Then the array is named reuse. Then further down the random number will actually be reuse which is the array which is the original number. Am I correct? I have been putting this code all over in my php file with no luck. Any other help you can give me would be fantastic.

Next Topics on Trending Feed

  • 6

    Ok So I have a slider on my site and then under that there are 3 boxes. If a post has a featured image it will then show that picture on the slider and in the box if its the first post. Well when there is not post it shows a random picture from 8 different pictures. The problem I have is that the random picture that correlates to the first post is not the same in the slider and the box. Below is the code. How would I fix this? Code: if (have_posts()) : while (have_posts()) : the_post(); if($img_position == 'boxgrid'){ $thumb = get_the_post_thumbnail( $post->ID, 'post-thumbnail' ); $pattern= "/(?<=src=['|\"])[^'|\"]*?(?=['|\"])/i"; preg_match($pattern, $thumb, $thePath); $random_number = mt_rand(1, 8); if(!isset($thePath[0])){ $thePath[0] = get_template_directory_uri().'/images/slideshow/noftrdimg-'.$random_number.'.jpg'; } $tmp .= '<div class="boxgrid captionfull" style="background: transparent url('.$thePath[0].') repeat scroll 0 0; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; " title="'. get_the_title().'">'; $tmp .= '<div class="cover boxcaption">'; $tmp .= '<h3 style="padding-left:8px;"><a href="'. get_permalink().'" title="'. get_the_title().'">'. get_the_title().'</a></h3>'; $tmp .= '<p>'.substr(get_the_excerpt(), 0, 100).'</p>'; $tmp .= '</div>'; $tmp .= '</div>'; So that is the little boxes then here is for the slider. They are both in the same php file