Support

Account

Home Forums General Issues Randomize array content in PHP via WordPress posts

Unread

Randomize array content in PHP via WordPress posts

  • 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 ): ?>
    <a href=”<?php the_field(‘home_link’, $banner->ID ); ?>”><?php the_field( ‘home_headline’, $banner->ID ); ?></a>
    <?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.

Viewing 1 post (of 1 total)

The topic ‘Randomize array content in PHP via WordPress posts’ is closed to new replies.