Display only entire most recent post on author page?

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

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="https://wordpress.stackexchange.com/questions/312855/<?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.

Leave a Comment