0

There is a problem by using the_author(); in file author.php I use this code to display author name heading

<h1 class="page-title"><a>آخرین اخبار</a> <a href="<?php the_author_url(); ?>" target="_blank"><?php the_author(); ?></a></h1>

but the title and url are wrong and belong to another author! Notice : this code is outside of loop

1 Answer 1

1

Sorry, but I found the answer very quickly myself and I attempted to delete this original question, but I think maybe somebody else may need this answer.

This is right code to solve this issue:

<h1 class="page-title">  <?php echo '<a href="'.get_author_posts_url(get_userdata($posts[0]->post_author)->data->ID).'">'.get_userdata($posts[0]->post_author)->data->display_name.'</a>'; ?></h1>
1
  • 1
    the post_author is already the ID, so you can simplify it to get_author_posts_url( $posts[0]->post_author ), however, this answer implies you're using get_posts or a custom WP_Query to replace or change the posts shown on your author archives, and that the loop you spoke of is not a main loop but a secondary loop, which would go a long way to explaining your problem. If you added more information to your question to confirm this and share the loop it might be possible to make the_author and the_author_url work as you expected and speed up that page
    – Tom J Nowell
    Commented Sep 25, 2022 at 8:08

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.