0

Is there a way to run the loop as usual (showing all the posts), rewind the posts and then do a secondary loop (if one is actually needed) to show only the first post in an archive template?

My requirement is to setup a template which generates a list of all the current category's posts in a sidebar and then show only the first post in the main content area.

Something along these lines:

        <?php if (have_posts()) : ?>
            <div class="sidebar">
                <ul>
                    <?php while (have_posts()) : the_post(); ?>
                        <li>
                            <a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                        </li>
                    <?php endwhile; ?>
                </ul>
            </div>
            <?php rewind_posts() ?>
            <div class="maincontent">
                <?php while (have_posts()) : the_post(); ?>
                    <?php the_content() ?>
                <?php endwhile; ?>
            </div>
        <?php endif; ?>
0

There if few ways to achieve what you need:

Option 1:

Add break after the_content() in the main content loop.

Option 2:

You can do something like this but I don't think so that it is a good solution.

<?php if (have_posts()) :
    $first_post = true;
    ?>
    <div class="sidebar">
        <ul>
            <?php while (have_posts()) : the_post(); ?>
                <?php
                    if ( $first_post ) {
                        $first_post_content = get_the_content();
                        $first_post = false;
                    }
                ?>
                <li>
                    <a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </li>
            <?php endwhile; ?>
        </ul>
    </div>
    <?php rewind_posts() ?>
    <div class="maincontent">
        <?php echo $first_post_content; ?>
    </div>
<?php endif; ?>

Option 3:

Second query. In my opinion the best solution.

<?php if (have_posts()) : ?>
    <div class="sidebar">
        <ul>
            <?php while (have_posts()) : the_post(); ?>
                <li>
                    <a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </li>
            <?php endwhile; ?>
        </ul>
    </div>
    <?php rewind_posts() ?>
    <div class="maincontent">
        <?php $the_query = new WP_Query( array( 'posts_per_page' => 1 ) ) ?>
        <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
            <?php the_content(); ?>
        <?php endwhile; wp_reset_postdata() ?>
    </div>
<?php endif; ?>
  • BTW the proper solution of this problem is to display all posts in the sidebar using WP_Query and then modify main query using pre_get_posts filter for this specific archive. – Tomasz Bakula Oct 2 '16 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.