WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Filespit
    Member
    Posted 1 year 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; ?>

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

  2. Filespit
    Member
    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year ago #

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

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

  4. Filespit
    Member
    Posted 1 year 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

About this Topic