Support

Account

Home Forums Front-end Issues Showing one random post object from a sub_field list

Solved

Showing one random post object from a sub_field list

  • Hi, i’m trying to show a product from a list filed with a repeater field on a post object. I’m going nowhere right now, tried to shuffle, to array,…But i’m stuck !

    Here is what my code looks like. At the time i’m showing all products (sub_fields), but i just need one, who might be different anytime you reload the page.

    Thanks for your help !

    <?php
    
    if( have_rows('products')):
    
    	echo "<section class='shopThis'>
    			<h3 class='title md-col-3'>Shop this in our store</h3>
    			<!--<p>This article is available in our brand new online store.<br>-->
    			<p>We brought a few things back from this trip, and it's now available in our brand new online store.<br />
    			Gets yours by clicking here!</p>
    			
    			<div class='articles_container'>";
    	while ( have_rows('products') ) : the_row();?>
    
    				<?php
    				$post_object = get_sub_field('product_image');
    
    				if( $post_object):
    					$post = $post_object;
    					setup_postdata($post);
    					?>
    
    			<article>
    				<a href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?></a>
    				<a href="<?php echo the_permalink();?>" class="btn">shop this</a>
    			</article>
    
    					
    
    				<?php endif;?>
    		<?php wp_reset_postdata();?>
    
    <?php 	endwhile;
    
    echo "</div>
    	</section>";
    
    endif;
    
    ?>
    
  • Hi @qhuit

    In this case, I suggest you use the get_field() function and show the random row using the array_rand() function. It should be something like this:

    $repeater = get_field('products');
    $post_object = $repeater[array_rand($repeater)]['product_image'];
    
    if( $post_object){
        // do something with the $post_object
    }

    I hope this helps 🙂

  • Hi @James and thanks a lot for your answer, you (partially) saved me !!

    The only problem I’ve got is that it sometime shows one product many times…Any idea ?

    <?php
    
    if( have_rows('products')):
    
    	echo "<section class='shopThis'>
    			<h3 class='title md-col-3'>Shop this in our store</h3>
    			<!--<p>This article is available in our brand new online store.<br>-->
    			<p>We brought a few things back from this trip, and it's now available in our brand new online store.<br />
    			Gets yours by clicking here!</p>
    			
    			<div class='articles_container'>";
    	while ( have_rows('products') ) : the_row();?>
    
    				<?php
    		$repeater = get_field('products');
    		$post_object = $repeater[array_rand($repeater)]['product_image'];
    
    				if( $post_object):
    					$post = $post_object;
    					setup_postdata($post);
    					?>
    
    			<article>
    				<a href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?></a>
    				<a href="<?php echo the_permalink();?>" class="btn">shop this</a>
    			</article>
    
    				<?php endif;?>
    		<?php wp_reset_postdata();?>
    
    <?php 	endwhile;
    
    echo "</div>
    	</section>";
    
    endif;
    
    ?>
    
    <?php
    /*
    
    if( have_rows('products')):
    
    	while ( have_rows('products') ) : the_row();*/?><!--
    
    		<?php
    /*		$post_object = get_sub_field('product_image');
    		if( $post_object):
    
    			$i = 0;
    			foreach( $post_object as $post):
    			$post = $post_object;
    			setup_postdata($post);
    			array_rand((array)$post);
    			*/?>
    
    			<a href="<?php /*echo the_permalink();*/?>"><?php /*the_post_thumbnail();*/?></a>
    	<?php /*if (++$i == 2) break;*/?>
    
    		<?php
    /*				wp_reset_postdata();
    			endforeach;
    
    		endif;*/?>
    
    	--><?php /*	endwhile;
    
    endif;
    
    */?>
    

    Thanks again for your help 🙂

  • Hi @qhuit

    That’s because you were still looping through the repeater field. If you want to show only one row, then you don’t need to loop through the repeater data. It should be something like this:

    <?php
    $repeater = get_field('products');
    $post_object = $repeater[array_rand($repeater)]['product_image'];
    
    if($post_object):
    
    	echo "<section class='shopThis'>
            <h3 class='title md-col-3'>Shop this in our store</h3>
            <!--<p>This article is available in our brand new online store.<br>-->
            <p>We brought a few things back from this trip, and it's now available in our brand new online store.<br />
            Gets yours by clicking here!</p>
            
            <div class='articles_container'>";
    
        $post = $post_object;
        setup_postdata($post);
        ?>
    
        <article>
            <a href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?></a>
            <a href="<?php echo the_permalink();?>" class="btn">shop this</a>
        </article>
    
        <?php wp_reset_postdata();
    
        echo "</div>
            </section>";
    
    endif; ?>

    Hope this helps 🙂

  • @James you’re my hero !

    And…what if i want to show 4 (different) products instead of just one ? 😀
    That will be my last question, I promise !!

    Thanks again for answering me !

  • Hi @qhuit

    In this case, you need to pass the total number of the products to the array_rand() function like this:

    <?php
    $repeater = get_field('products');
    
    // get 4 keys of the repeater
    $repeater_keys = array_rand($repeater, 4);
    
    if($repeater):
    
    	echo "<section class='shopThis'>
            <h3 class='title md-col-3'>Shop this in our store</h3>
            <!--<p>This article is available in our brand new online store.<br>-->
            <p>We brought a few things back from this trip, and it's now available in our brand new online store.<br />
            Gets yours by clicking here!</p>
            
            <div class='articles_container'>";
        
        // loop through the keys
        foreach( $repeater_keys as $repeater_key ){
            
            //get the post object using the repeater key
            $post_object = $repeater[$repeater_key]['product_image'];
                
            if( $post_object ){
                $post = $post_object;
                setup_postdata($post);
                ?>
    
                <article>
                    <a href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?></a>
                    <a href="<?php echo the_permalink();?>" class="btn">shop this</a>
                </article>
                
                <?php
            }
            
        }
        
        wp_reset_postdata();
        
        echo "</div>
            </section>";
    
    endif; ?>

    Hope this helps 🙂

  • YES ! That’s perfectly what I needed James ! Thanks a lot, you made my day !

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Showing one random post object from a sub_field list’ is closed to new replies.