Simple Fields
[resolved] Simple Fields - Display fields in random order (4 posts)

  1. Filespit
    Posted 3 years ago #

    Hello there.

    I am using the Simple Fields plugin on my website and I'd like to do the following;

    Let's say I have 10 simple fields on my page. I want to limit it down so the page only displays 2 of these, which are randomly selected each time the page loads. Is this possible? If yes, how do I do that?

    Here is my code;

    <?php $c = simple_fields_get_post_group_values($post->ID, "Users", true, 2); ?>
    <?php foreach ($c as $b) : ?>
      <?php echo wp_get_attachment_image ($b["Image"], "full"); ?>
      <?php echo $b["Text"] ?>
    <?php endforeach; ?>


  2. Filespit
    Posted 3 years ago #

    I solved the randomizing problem. I simply added shuffle($c); to the first line of code. Still can't figure out how to only display 2 posts instead of all.

  3. Pär Thernström
    Plugin Author

    Posted 3 years ago #

    To only get 2 items from the array you can probably use array_slice():


  4. Filespit
    Posted 3 years ago #

    Hi Pär! Thanks for answering. I managed to solve this issue with the following code. Hope this helps someone else as well!

    <?php $c = simple_fields_get_post_group_values($post->ID, "Users", true, 2); shuffle($c); foreach ($c as $b) {
      $i++; if($i < 3) { ?>
        <?php echo wp_get_attachment_image ($b["Image"], "full"); ?>
        <?php echo $b["Text"] ?>
     <?php } } ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Simple Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic