Home Forums ACF PRO Get author posts link from user field


Get author posts link from user field

  • I have a user field which I need to be set to return an array. It is also set to multiple=false.

    I need to display a link via the page template structured as follows:

    <a href="[author posts archive for user]">[user display_name]</a>

    I have found it very hard to get the posts link in any way other than by setting the field to return the user ID and hard-coding the link structure like this:

    $user_id = get_field('user');
    if( $user_id ): ?>
      <a href="<?php echo get_author_posts_url($user_id); ?>">[can't fetch display_name from ID]</a>
    <?php endif; ?>

    But this does not allow me to fetch the other data I need.

    How can I set this up so that I can fetch the author_posts_url AND the display_name pertaining to the user names in the field?

  • I have figured this out. The solution is as below, with the user field set to return “User Array”:

    <?php $author_id = get_field('user')['ID']; ?>
    by <a href="<?php echo get_author_posts_url($author_id) ?>" rel="author"><?php echo get_field('user')['display_name']; ?></a>

    Credit to @imvrk at this thread for the steer:

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.