2

Is there a way to display only the most recent post in its entirety on the author web page instead of all the posts? In other words, I would like the author web page to look just like a single post page but showing the most recent post for that author. For example, www.mydomain.com/author/author-name should only display the author's most recent post and not a collection of their post excerpts.

1 Answer 1

0

First, you need to create an author.php file, or edit the one that your theme has, in my case, I did the test with the twentyseventeen and it doesn't have an author.php file, so I created it.

<?php
/**
 * The template for displaying all single posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @version 1.0
 */

get_header(); ?>

    <?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_id) : get_userdata(intval($author));
    ?>

<div class="wrap">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

<?php 
global $query_string;
query_posts( $query_string . '&posts_per_page=1' );

while (have_posts()) : the_post(); ?>
     <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
     <?php the_content(); ?>
<?php endwhile;?>

        </main><!-- #main -->
    </div><!-- #primary -->
    <?php get_sidebar(); ?>
</div><!-- .wrap -->

<?php
get_footer();

The key section is:

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_id) : get_userdata(intval($author));

In which we are getting the current user, and then we show the last post and since we only want the last one we say posts_per_page=1.

2
  • Thank you for your quick reply. I kind of got it working but Wordpress.org says the query_posts function "can be problematic and should be avoided wherever possible"
    – HBCondo
    Commented Aug 30, 2018 at 7:41
  • In a case like this, I wouldn't afraid to use it, it won't have any conflict with anything since this code will only work on the/author page. Commented Aug 30, 2018 at 12:04

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.