WordPress.org

Forums

Random page children (2 posts)

  1. Arnii
    Member
    Posted 5 years ago #

    Hi!

    I'm struggling to make a loop where, let's say 4, children of a certain page ID is shown at a footer.

    I can make the loop with posts, but cannot figure out how to archive this functionality with pages...

    The code working for posts, looks like this;

    <?php
    $querystr = "SELECT * FROM (SELECT wposts.* FROM $wpdb->posts wposts WHERE wposts.post_status = 'publish' ORDER BY rand()) as y limit 4 ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php //setup_postdata($post); ?>
    
          <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    
        <?php endforeach; ?>
     <?php endif; ?>

    Any help or guidance will be appreciated!

    Thanks!

  2. Arnii
    Member
    Posted 5 years ago #

    Okay, I almost got it;

    <?php
    $rand_module = get_posts('static=true&child_of=37&number=2&orderby=rand');
    foreach( $rand_module as $post ) :
    ?>
    DO STUFF
    <?php endforeach; ?>

    But somehow the number=2 doesn't work. The function gives me ALL of the children in random order. Anyone know how to limit this?

    Thanks...

Topic Closed

This topic has been closed to new replies.

About this Topic