Home › Forums › Front-end Issues › Displaying Field Labels
Hello,
I’m using shortcodes to display custom field info on my site – is there a way to show the field label as well?
Many thanks!
Not with ACF’s built in short code. You would need to build a custom short code.
Hi ! I retake this thread because I would really appreciate if you could give some piece of information on how we could possibly create a shortcode to show the field label instead of the field value. Is it complex ? I’m sure this could be useful for other people too.
Many thanks.
Xavier
Basically, you’d need to create your own shortcode
It’s described here in the WP Codex: https://codex.wordpress.org/Shortcode_API
Here is the function from ACF
function acf_shortcode( $atts )
{
// extract attributs
extract( shortcode_atts( array(
'field' => '',
'post_id' => false,
'format_value' => true
), $atts ) );
// get value and return it
$value = get_field( $field, $post_id, $format_value );
if( is_array($value) )
{
$value = @implode( ', ', $value );
}
return $value;
}
add_shortcode( 'acf', 'acf_shortcode' );
You’d need to decide what additional things you’d want to be able to do and set up the attributes. Then you’d need to research the ACF functions and filters to that will allow you to get other information about a field and display it.
Honestly, not that complex. For example you can get all the other field data by calling get_field_object(), you can read more about this function here: http://www.advancedcustomfields.com/resources/get_field_object/
The difficult part is not getting the data but formatting it. It’s easy to get a value and just output it, but if you’re going to get the label and the value or any additional information, for example, what if there is no value, do you output the default? If there is not value to you still output the label? You’re going to have to provide ways for developers to format what the field will look like, as well as what the default is.
Hope this helps.
Great ! what a reactive support !
I’ll check all your above material.
Many thanks really.
The topic ‘Displaying Field Labels’ is closed to new replies.
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.