Loop inside the loop

I would use get_posts() for your two ad hoc Loops. (Actually, you only need one ad hoc Loop, if you’re modifying the main Loop so that only 2 posts are retrieved.) e.g.

\\ code modify main Loop to return only 2 posts goes here

$firstpost = true;

if ( have_posts() ) : while ( have_posts() ) : the_post();

    \\ main Loop Post output goes here

    if ( $firstpost ) {

        $randomposts = get_posts( array( 'category' => $categoryid, 'numberposts' = $numberposts, 'orderby' => 'rand' );

        \\ Output ad-hod Loop content, e.g.
        ?>
        <ul>
        <?php
        foreach ( $randomposts as $randompost ) { ?>
            <li><a href="https://wordpress.stackexchange.com/questions/17604/<?php echo get_permalink( $randompost->ID ); ?>"><?php echo $randompost->post_title; ?></a></li>
        <?php } ?>
        </ul>
    <?php }

   $firstpost = false;

// end the main Loop
endwhile; endif;

(Untested example code)

Leave a Comment