
Do you guys know how to limit posts per page and randomize posts in wordpress?
I have a relationship field in the back-end where I add and remove items that I created to display in a website. This content is printed through the WP_Query below:
<?php
$args = array (
‘post_type’ => ‘home_banners’
$fullbanner = new WP_Query ( $args );
?>
And here is the PHP:
<?php if ( have_posts() ) : while ( $fullbanner->have_posts() ) : $fullbanner->the_post(); ?>
#####Get the relationship field
<?php $banners = get_field(‘home_banner_01_selection’); ?>
#####Check if the relationship field has contents
<?php if( $banners ): ?>
#####Start foreach
<?php foreach( $banners as $banner ): ?>
ID ); ?>”><?php the_field( ‘home_headline’, $banner->ID ); ?>
<?php endforeach; ?>
<?php endif; ?>
I have three contents added in this relationship field. I want just to display ONLY ONE content per page and randomize it when refreshing the page. At the moment it is currently display all the three contents in the page.
I notice that the var $banners
behave as an array
. If I add echo count($banners);
it will display 3. Moreover, if I add shuffle($banners);
it will shuffle the content among them.
Thanks for helping me.
Something like this should get you looking in the right direction.
<?php if ( have_posts() ) : while ( $fullbanner->have_posts() ) : $fullbanner->the_post(); ?>
<?php $banners = get_field(‘home_banner_01_selection’); ?>
<?php if( $banners ): ?>
<?php shuffle($banners); ?>
<?php foreach(array_slice($banners, 0, 1) as $banner ): ?>
<?php the_field( 'home_headline', $banner->ID ); ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>