简体   繁体   中英

How do I get a link to the authors first post in wordpress

I am trying to add a link to an author's first posts (which is designed to be the author profile) to all other posts by that author.

I am trying

a href="<?php echo get_author_posts_url(get_the_author_meta( '1' )); ?>"><?php the_author(); ?></a>

I am just a designer who is not much experienced with PHP, this is a client request which I am unable to solve with any plugins but I am sure will be a walk in the park for a pro.

your help will be very much appreciated and could save me losing a client.

This should work...

<?php 
$firstPostQuery = new WP_Query([
    "author" => 1, "posts_per_page" => 1, "orderby" => "date", "order" => "ASC", "fields" => "ids"
]);

if($firstPostQuery->post_count > 0) {
    $firstPostID = $firstPostQuery->posts[0];
    ?><a href="<?php echo get_permalink($firstPostID); ?>"><?php the_author(); ?></a>
<?php } ?>

This first fetches the post ID for author's first post. On a successful query, the code goes on to echo the anchor tag that you had in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM