Skip Navigation

[Resolved] Adding custom field to author archive

This support ticket is created 2 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by Christopher Amirian 2 years, 3 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2593893
Screenshot 2023-04-14 at 1.22.36 PM.png

Tell us what you are trying to do? I want to add a custom field (a bio field, already created) to display on the author archive.

Is there any documentation that you are following? Yes. https://toolset.com/forums/topic/how-to-display-users-custom-fields-on-author-archive/

I followed the instructions in the documentation above. I created a fields plus text block and entered the shortcode as shown above using the edit html capability of the block.

I found that this worked fine if the block was below the "wordpress archive loop" tag but did not work when above that tag but still within the wparchive. I ended up adding the same block 3 times so you can see the effect. Also note the attached image.

The problem with putting it below the loop tag is it repeats for every post, whereas it is meant to be a one time display at the top. I feel I am doing something dumb. Please help. 🙂

Is there a similar example that we can see?

What is the link to your site?hidden link

#2594765

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

That is correct, the custom field can not be shown before the loop because it is not available to get the information from when there is no loop.

Also, by adding custom field you will have that field available on all single posts of the post type you assigned the custom field in. So it is logical that you will have items inside the loop itself.

I suggest that you use the custom field inside the loop and use a Conditional block to show the field when it is not empty:

https://toolset.com/course-lesson/using-toolset-conditional-block/

Then add the value which you want to show only to the first item in the post and not other posts. Keep the other posts have an empty value for the customer field.

Thanks.

#2594825

That makes sense but I think there is one thing that might change your reply, so let me explain.

The custom field is attached to the user (it's a user field) and not to a post type. For example it is a field where the user has entered their bio. Thus, I want it to appear at the top of the archive before the loop.

I know the page knows who the author is before the loop, because it display the user photo and description fields (WP native fields). So, I am trying to figure out how to make the user custom field "bio" appear near the top of the author archive before the loop.

Any ideas how to accomplish that?

#2595237

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I checked that on my installation and it worked to show a user custom field above the WordPress Archive Output item but it will not work outside of the main WordPress Archive block item as that is the main wrapper that Toolset understands in the archive.

Please use the Fields and Text block and add the user custom field and select the current logged in user for that.

Thank you.

#2596655

I must be missing something. My goal is to have the user field of author Fred appear on author Fred's archive page above the loop of articles (ie his bio) when the archive is viewed by someone else.

If I use the Fields and text block and choose current user, it shows as a field that sometimes is blank and sometimes displays the bio of the admin (ie me) but does not show the user field (bio) of the author whose archive it is. This is the shortcode: [types usermeta='tel_user_bio' current_user='true'][/types]

This makes sense. I believe current user should show the field belonging to the logged in user, whereas my goal is the show the field belonging to the author archive user.

Previously I had tried the shortcode shown in the how to article above but it only seemed to work inside the archive loop. What am I missing?

#2596657

Here is the shortcode that does what I want if it's in the loop but not above the loop. [types usermeta='tel_user_bio' user_id='[get-author-id-in-archive][/get-author-id-in-archive]'][/types]

#2596901

Christopher Amirian
Supporter

Languages: English (English )

Screenshot 2023-04-19 at 14.17.42.png

Hi there,

Thank you now I get a better picture of the request. Please consider that you can not add a shortocde outside the WordPress Archive block but you can add it above the loop itself. (check the screenshot)

Please change the code in the shortcode to this:

function get_author_id_in_archive_func($atts) {

  $author = get_user_by( 'slug', get_query_var( 'author_name' ) );

  return $author->ID;
}
add_shortcode("get-author-id-in-archive", "get_author_id_in_archive_func");

And then use the same shortcode that you used in the archive itself above the Loop element.

I tested and it worked for me:

hidden link

Thanks.