Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1047725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:20:47+00:00 2026-05-16T16:20:47+00:00

I have the following code that shows 5 posts each being the latest from

  • 0

I have the following code that shows 5 posts each being the latest from an author. Want I want to do is show the these 5 posts in a random order so that no author gets precedence over the other. To clarify this is the ordering of these 5 posts and NOT the posts of the author. Thanks

code:

<?PHP

  get_header();

 ?>


 <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.masonry.js"></script>
 <script type="text/javascript">
  jQuery(document).ready(function(){
   $('#post-list').masonry({ singleMode: true, itemSelector: 'article', animate: false });
  });
 </script>

 <?php

  function MyLoopCode()
  {
 ?>

 <article id="post-<?php the_ID(); ?>">

  <div class="post-image"></div>

  <div class="post-text">

  <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

  <p class="p-cat">In: <?php the_category('|') ?></p>

  <p class="p-author">
   <span class="name"><?php the_author_posts_link(); ?></span>
   <span class="avatar"><a title="View posts by <?php the_author(); ?>" href="<?php echo get_author_posts_url($authordata->ID); ?>"><?php echo get_avatar( $email, $size = '64' ); ?></a>
   </span>
  </p>


  <small class="p-time">
  <strong class="day"><?php the_time('j') ?></strong>
  <strong class="month"><?php the_time('M') ?></strong>
  <strong class="year"><?php the_time('Y') ?></strong>
  </small>

  <section class="content">
   <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?>
  </section>

  <div class="p-det">
   <p class="p-det-com"><?php comments_popup_link('No Comments', '(1) Comment', '(%) Comments'); ?></p>
   <?php if (function_exists('the_tags')) { ?> <?php the_tags('<p class="p-det-tag">Tags: ', ', ', '</p>'); ?> <?php } ?>
  </div>

  </div>

 </article>

 <?php } ?>



   <div id="maincontent" class="clearfix">

    <div class="leftcontent">

     <section id="post-list" class="post-list">

     <?php //query_posts('orderby=rand'); ?>

     <?php query_posts('posts_per_page=1&author=2'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=3'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=4'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <article>

      <p>ADVERTISEMENT</p>

     </article>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     <?php rewind_posts(); ?>

     <?php query_posts('posts_per_page=1&author=6'); if (have_posts()) : while (have_posts()) : the_post(); ?>

      <?php echo MyLoopCode(); ?>

     <?php endwhile; endif; ?>

     </section>

    </div>
    <!-- END div.leftcontent -->

    <?php get_sidebar(); ?>

   </div>
   <!-- END div#maincontent -->



 <?PHP

  get_footer();

 ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:20:48+00:00Added an answer on May 16, 2026 at 4:20 pm

    Summary:
    1. Get output of MyLoopCode() as an array.
    2. Shuffle the array.
    3. Display contents.


    Implementation:

    1) Return the output of MyLoopCode() using ob_start() and ob_get_clean() & store it in an array.

    Explanation: The ob_start() starts output caching so instead of sending the output to the browser the PHP will keep the output in its buffer. Then, at the end of the function, by using ob_get_clean(), we tell PHP to give use the output as a string and delete from its buffer. So, the function now returns the contents that would be otherwise outputted to the browser by the MyLoopCode() function.

    <?php
     function MyLoopCode()
      {
    ob_start();
     ?>
    
     <article id="post-<?php the_ID(); ?>">
    
      <div class="post-image"></div>
    
      <div class="post-text">
    
      <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
      <p class="p-cat">In: <?php the_category('|') ?></p>
    
      <p class="p-author">
       <span class="name"><?php the_author_posts_link(); ?></span>
       <span class="avatar"><a title="View posts by <?php the_author(); ?>" href="<?php echo get_author_posts_url($authordata->ID); ?>"><?php echo get_avatar( $email, $size = '64' ); ?></a>
       </span>
      </p>
    
    
      <small class="p-time">
      <strong class="day"><?php the_time('j') ?></strong>
      <strong class="month"><?php the_time('M') ?></strong>
      <strong class="year"><?php the_time('Y') ?></strong>
      </small>
    
      <section class="content">
       <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?>
      </section>
    
      <div class="p-det">
       <p class="p-det-com"><?php comments_popup_link('No Comments', '(1) Comment', '(%) Comments'); ?></p>
       <?php if (function_exists('the_tags')) { ?> <?php the_tags('<p class="p-det-tag">Tags: ', ', ', '</p>'); ?> <?php } ?>
      </div>
    
      </div>
    
     </article>
    
     <?php
      return ob_get_clean();
      } ?>
    

    2) Now, instead of echoing the output directly, as I said save it in an array:

    Explanation: Everytime the MyLoopCode() function is called, it output is now stored in the array $myarray. So, NO output is sent to the browser yet.

    <?php query_posts('posts_per_page=1&author=2'); if (have_posts()) : while (have_posts()) : the_post(); ?>
    
      <?php $myarray[] = MyLoopCode(); ?>
    
    <?php endwhile; endif; ?>
    

    After all these function calls, the content of $myarray will look like (pseudo-code):

    myarray[0] = user1-post1 + user1-post2 + user1-post3 + user1-post4 + user1-post5;  
    myarray[1] = user2-post1 + user2-post2 + user2-post3 + user2-post4 + user2-post5;  
    myarray[2] = user3-post1 + user3-post2 + user3-post3 + user3-post4 + user3-post5;  
    myarray[3] = user4-post1 + user4-post2 + user4-post3 + user4-post4 + user4-post5;  
    myarray[4] = user5-post1 + user5-post2 + user5-post3 + user5-post4 + user5-post5;  
    

    3) Now, randomize the array contents using shuffle() and display them:

    Explanation: The shuffle() function randomizes the contents of the $myarray. Since this array contains all the posts by individual user, what really happens is that the posts-user groups are randomized. Finally traverse the array by foreach and echo the contents.

     <?php
      shuffle($myarray);
    
      foreach($myarray as $x)
            echo $x;
     ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.