Home › Forums › General Issues › Shortcode: Using the Hide if empty in a shortcode › Reply To: Shortcode: Using the Hide if empty in a shortcode
You have to build your own shortcode. This is a copy of the acf shortcode modified to create output if not empty.
function acf_shortcode_if_value( $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 (empty($value)) {
return '';
}
// array
if( is_array($value) ) {
$value = @implode( ', ', $value );
}
$value = '<p>My field value: '.$value.'</p>';
// 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.