0

Here is the loop from my author.php page. It is showing recent posts, but not the MOST recent.

Where is my mistake?

    <?php if (have_posts()): the_post(); ?>
        <h3>
            <?php _e('All posts by'); ?> <?php echo get_the_author(); ?>
            <span class="arrows">&raquo;</span>
        </h3>

    <?php while (have_posts()) : the_post();?>

    <div id="post-<?php the_ID(); ?>" class="cat-post">
    <div class="cat-post-left">
        <a href="<?php the_permalink() ?>" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title(); ?>"><?php the_post_thumbnail( '300x169' ); ?></a>
        <div class="meta-box">
        <div class="post-meta">
<div class="line"><cite>By &nbsp;<span class="plus-icon"><?php the_author_posts_link(); ?></span></cite><span><?php the_date(); ?></span></div>
</div>
</div>
</div>
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
                <a class="continue-reading" href="<?php the_permalink(); ?>">Read on &raquo;</a>
            </div>

Here is the site. The "Editor's Pick" post on the homepage is the most recent and does not show up on the Author page.

2 Answers 2

2

The mistake is in your first block of code:

<?php if (have_posts()): the_post(); ?>
    <h3>
        <?php _e('All posts by'); ?> <?php echo get_the_author(); ?>
        <span class="arrows">&raquo;</span>
    </h3>

<?php while (have_posts()) : the_post();?>

You call the_post() to populate the regular template tags (i.e. get_the_author()) but don't use the rest of the post. Then, inside your while loop, you call the_post() to populate these template tags with the next post in the result set.

Instead, you need to handle the first post in that first block as well. Change your code to something like this:

<?php if (have_posts()): the_post(); ?>
    <h3>
        <?php _e('All posts by'); ?> <?php the_author(); ?>
        <span class="arrows">&raquo;</span>
    </h3>

    <!-- markup for first post -->
    <div id="post-<?php the_ID(); ?>" class="cat-post">
        <div class="cat-post-left">
            <a href="<?php the_permalink() ?>" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title(); ?>"><?php the_post_thumbnail( '300x169' ); ?></a>
            <div class="meta-box">
                <div class="post-meta">
                    <div class="line"><cite>By &nbsp;<span class="plus-icon"><?php the_author_posts_link(); ?></span></cite><span><?php the_date(); ?></span></div>
                </div>
            </div>
        </div>

        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <?php the_excerpt(); ?>
        <a class="continue-reading" href="<?php the_permalink(); ?>">Read on &raquo;</a>
    </div>

<?php while (have_posts()) : the_post();?>

    <!-- markup for subsequent posts -->
    <div id="post-<?php the_ID(); ?>" class="cat-post">
        <div class="cat-post-left">
            <a href="<?php the_permalink() ?>" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title(); ?>"><?php the_post_thumbnail( '300x169' ); ?></a>
            <div class="meta-box">
                <div class="post-meta">
                    <div class="line"><cite>By &nbsp;<span class="plus-icon"><?php the_author_posts_link(); ?></span></cite><span><?php the_date(); ?></span></div>
                </div>
            </div>
        </div>

        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <?php the_excerpt(); ?>
        <a class="continue-reading" href="<?php the_permalink(); ?>">Read on &raquo;</a>
    </div>
1

This might be better handled with using rewind_posts();. I've used this to get around this problem recently, and it's worked well. It simply rewinds the loop back to the first post (http://codex.wordpress.org/Function_Reference/rewind_posts).

I've suggested (untested) code below.

<?php rewind_posts(); ?>
<?php if (have_posts()): the_post(); ?>
    <h3>
        <?php _e('All posts by'); ?> <?php echo get_the_author(); ?>
        <span class="arrows">&raquo;</span>
    </h3>

<?php while (have_posts()) : the_post();?>

<div id="post-<?php the_ID(); ?>" class="cat-post">
<div class="cat-post-left">
    <a href="<?php the_permalink() ?>" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title(); ?>"><?php the_post_thumbnail( '300x169' ); ?></a>
    <div class="meta-box">
    <div class="post-meta">
<div class="line"><cite>By &nbsp;<span class="plus-icon"><?php the_author_posts_link(); ?></span></cite><span><?php the_date(); ?></span></div>
</div>
</div>
</div>
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php bloginfo('name'); ?> - <?php bloginfo('description'); ?> - <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
                <a class="continue-reading" href="<?php the_permalink(); ?>">Read on &raquo;</a>
        </div>
1
  • This should be an accepted answer, it provides a much better/cleaner solution. Commented Jul 1, 2014 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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