• I hope somebody can help me.

    What I am try to achieve is on my homepage to have 12 random images and on hover a random testimonial will swap out the image.(a kind of div flip)
    The 12 random images will come from galleries attached to pages which are children of a certain page id, and the testimonials are a separate post type.
    I have tried with array merge but each foreach/while (loop) gives me either or. Not both a result from my page and testimonial

    Can anyone point me in the right direction please.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Are you able to post the code you are using?

    Have you tried using array_push? It may be a possibility.

    Much simpler. Run each of your loops separately, saving the results in arrays. Then merge generate your output, fetching from the arrays.

    Thread Starter martiniboy

    (@martiniboy)

    Thanks for your help

    This is how I have managed it

    <?php 
    
    $quotes = array( 'post_type' => 'testimonial
    ', 'posts_per_page' => 12, 'orderby' => 'rand' );
    // background-color: rgba(0,0,0,0.75);
    $quotes_query = new WP_Query( $quotes );
    
    // here you might wanna apply some sort of sorting on $result->posts
    
    ?>
    <?php if( $quotes_query->have_posts() ): ?>
    <div id="homepagegall" class="galleryimage">
    	<?php while( $quotes_query->have_posts() ) : $quotes_query->the_post();
    	//this is to create random background colours of text
    	$bgArray= array('#282E33', '#25373A', '#164852', '#495E67', '#FF3838','#FFFFFF');
    	//create a random number, then use the colour whos key matches the number
    	$myRand = rand(0,(6-1));
    	$val = $bgArray[$myRand];
    	 ?>
    		<div class="container left">
            <?php $image = array(get_field('gallery',425), get_field('gallery',427) );
    
    		 foreach( $image as $gallery ):
    		 $rand = array_rand($gallery, 1);?>
    				<img src="<?php echo $gallery[$rand]['sizes']['thumbnail']; ?>" alt="<?php echo $gallery[$rand]['alt'];  ?>" />
                 <?php    endforeach;?>
    			<div class="textbox"  style="background-color:<?php echo $val;?>">
                <strong><?php the_title();?></strong>
                <p class="text"><?php the_excerpt();?></p>
                </div>
    		</div>
    	<?php endwhile; ?>
        <div class="clear"></div>
    </div>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>

    Not very pretty as I am needing to manually put the id of the pages in get_field() callback but is working,
    Although I am getting the same image multiple times but only have a few uploaded – will look into non duplicates in the array

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Double query one loop’ is closed to new replies.