Home › Forums › Front-end Issues › Show field data on a page
I am trying to use shortcodes to display the data from a custom field on a page but it is not working.
Field name is “aparato1” and this is the shortcode that I insert in the page:
Aparato 1: [acf field="aparato1"]
In the “Location” section I tell you to show it on the page:
http://plandehigiene.donworri.com/sample-page
By God someone help me !!!
What type of field are you trying to display with a shortcode?
Is the field on the same page where you are adding the shortcode?
Its a simple text field. Created in user profile in backend. I need show this field in a Page in frontend.
Thanks
The field is not located on the same page where you are showing it. You need to supply the correct $post_id value.
[acf field="field_name" post_id="123"]
You are trying to get fields from a user.
Then… Paste that code in my page only??? I dont understand the post id….
I need show user’s data in a Page but data of logged user in thats moment. User logged in system.
I gave you a link that explains the post id to use for users
https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/
it is “user_” plus the user’s ID "user_{$user_id}"
And if it is the user who is currently logged into the system? What ID is written?
To do that you will need to create your own shortcode. The ACF shortcode is a simple thing. There is nothing built into it that will let you get the currently logged in user’s ID.
Ufffff… I dont know about programation… I think its an impossible mission for me hahaha…
add_shortcode('acf_current_user_field', 'acf_current_user_field_shortcode');
function acf_current_user_field_shortcode($atts) {
// extract attributs
extract(shortcode_atts(array(
'field' => '',
'format_value' => true
), $atts));
// get the current user
$user_id = get_current_user_id();
// get value and return it
$value = get_field($field, 'user_'.$user_id, $format_value);
// array
if( is_array($value) ) {
$value = @implode( ', ', $value );
}
// return
return $value;
}
It goes in your functions.php file.
It really wasn’t hard. I just copied the ACF code for the shortcode and modified it slightly to use the currently logged in user ID.
I’ll try tomorrow. May I ask you if I have problems??
Thank very much!!!! 😃😉
Ok I already pasted the code in the functions.php file of my theme (Divi). With ACF I created the fields that you see in the attached image.
How do I display the result of each field on a page?
Thank You very much friend.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.