• Resolved Filespit

    (@filespit)


    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; ?>

    http://wordpress.org/extend/plugins/simple-fields/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Filespit

    (@filespit)

    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.

    Plugin Contributor eskapism

    (@eskapism)

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

    http://php.net/manual/en/function.array-slice.php

    Thread Starter Filespit

    (@filespit)

    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 } } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Simple Fields – Display fields in random order’ is closed to new replies.