Home › Forums › General Issues › Shortcode: Using the Hide if empty in a shortcode
Hi
I’ve got a basic shortcode working fine (returns hello world where the [shortcut name] is inserted).
I now want to use the standard “return the field output but hide if empty” in the shortcode
<?php if( get_field(‘field_name’) ): ?>
<p>My field value: <?php the_field(‘field_name’); ?></p>
<?php endif; ?>
But can’t figure it out after googling for hours
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');
you need to use replace
[acf field="field_name"]
with
[acf_if_value field="field_name"]
I am using an ACF Option page and the above code doesn’t work. See below example…
[acf_if_value=”gv_address_2″ post_id=”options”]
How can I get the shortcode to work for option pages?
Still looking to have this addressed…
I am using an ACF Option page and the above code doesn’t work. See below example…
[acf_if_value=”gv_address_2″ post_id=”options”]
How can I get the shortcode to work for option pages?
There is no reason that the sortcode function I posted above should not work for options page fields.
My apologies had a typo.
It would be awesome if you could use the original shortcode and add hide_empty =”1″.
For example…
[acf field="field_name" post_id=”options” hide_empty="1"]
If hide_empty=”1″ then Hide shortcode
This would be awesome as a standard option in ACF.
Two other options that would be awesome is to add html wrapper and class
[acf field="field_name" post_id="options" hide_empty="1" html_tag="div" class="my-class"]
This would output
<div class="my-class">Field Value</div>
If no value then output nothing.
If class exists but not html tag then default to <span>
This would be really awesome as options to the ACF standard shortcode.
You could even add these options to Edit Field Group for each field.
Html Tag
Class
Hide empty
Values in these fields would be added to the ACF shortcode automatically. If blank, then you could add on the fly with custom shortcode.
You could even add these options to Edit Field Group for each field.
Html Tag
Class
Hide empty
Values in these fields would be added to the ACF shortcode automatically. If blank, then you could add on the fly with custom shortcode.
The problem is that you are talking about showing or not showing the containing HTML elements if there is not value. There are no shortcodes in ACF or WP that do this. The shortcode for ACF is meant to be extremely simple because it cannot cater to every possible use case. This is why we have the ability to create our own shortcodes and filters that deal specifically with out our own needs.
Rather than remembering to use another shortcode just add options to acf standard shortcode. If the option hide_empty doesn’t exist then nothing. That’s simple.
[acf field="field_name" post_id="options" hide_empty="1"]
This seems simple and I would venture to say many of your users already want this.
My code had a typo because I was not used to the new shortcode
[acf_if_value=”gv_address_2″ post_id=”options”]
Should have been
[acf_if_value field="gv_address_2" post_id="options"]
I probably would not have screwed this up if it were…
[acf field="field_name" post_id="options" hide_empty="1"]
Regrading wrapper html and class, if the options in the shortcode don’t exist, then nothing.
You already add separate wrapper class to style the backend admin site. When I first used ACF I thought that was what It was going to do, output to the page. Instead it was admin backend styling.
John, I heard your a wizard, could you provide hide_empty=”1″ code?
– Please 🙂
John, if not incorporating all options into acf standard shortcode
[acf field="field_name" post_id="options" hide_empty="1" html_tag="div" class="my-class"]
Then create another shortcode acf_html for adding the html
[acf_html field="field_name" post_id="options" hide_empty="1" html_tag="div" class="my-class"]
Would output:
<div class="my-class">Field Value</div>
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');
Hi @hube2 using your original code, how can I show label field too? Thanks.
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.