Home › Forums › General Issues › Shortcode: Using the Hide if empty in a shortcode › Reply To: Shortcode: Using the Hide if empty in a shortcode
we cannot modify the shortcode in ACF. The only option we have is to build our own shortcodes. I am not the developer of ACF.
Making changes to the shortcode I posted above is really quite simple. You just need to add the attributes you want and then use those attributes in the function. As an example, this one adds “html_before” and “html_after” which are similar attributes used in several WP functions.
function acf_shortcode_if_value( $atts ) {
// extract attributs
extract( shortcode_atts( array(
'field' => '',
'post_id' => false,
'format_value' => true,
'html_before' => '',
'html_after' => ''
), $atts ) );
// get value and return it
$value = get_field( $field, $post_id, $format_value );
if (empty($value)) {
return '';
}
// array
if( is_array($value) ) {
$value = @implode( ', ', $value );
}
$value = $atts['html_before'].''.$value.$atts['html_after'];
// return
return $value;
}
add_shortcode('acf_if_value', 'acf_shortcode_if_value');
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.