| | #1 |
| HyperActive Warrior Join Date: 2009 Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
|
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>'; 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); Thanks! |
| |
| | #2 |
| VIP Warrior War Room Member Join Date: 2006 Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
|
Are you new to programming? Save the random numbers generated in an array and reuse it in the second part. |
| |
| | #3 |
| HyperActive Warrior Join Date: 2009 Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
|
Yes quite new. Ok I will look into how to do that. Thanks!
|
| |
| | #4 |
| HyperActive Warrior Join Date: 2009 Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
|
I haven't found anything about how to do this online. Do you have anywhere you could point me?
|
| |
| | #5 |
| Software Product Creator War Room Member Join Date: 2012 Location: NL => Thailand
Posts: 452
Thanks: 42
Thanked 164 Times in 93 Posts
Blog Entries: 5 |
$reuse = array(); .... $reuse[] = $randnr; or you can use associative arrays.. see php.net for more explination |
| |
| The Following User Says Thank You to SteveSRS For This Useful Post: |
| | #6 |
| HyperActive Warrior Join Date: 2009 Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
|
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.
|
| |
|
| Bookmarks |
| Tags |
| image, random, sections, show |
| |