Home › Forums › Front-end Issues › Conditional shortcodes
Hi.
On a site using Visual Composer I want to not just omit outputting a field if it has no value but the whole row it is in. I thought this were something certainly included in ACF but couldn’t find it. So that’s the most basic thing I came up with:
// [acfcond field='']foobar[/afccond]
function acfcond($atts = [], $content = null)
{
$atts = array_change_key_case((array)$atts, CASE_LOWER);
if(get_field($atts['field']))
{
$content = do_shortcode($content);
}
else
{
$content = '';
}
return $content;
}
add_shortcode('acfcond', 'acfcond');
This way I can wrap text and also text containing other shortcodes as used in e.g. Visual Composer. If the specified field has no value, then the parts enclosed by this shortcode won’t be shown.
So… is there something built in to do this (probably better and smarter)? If not, are there better ways anyone has come up with?
Best regards
Floutsch
Hi Floutsch,
I don’t know if there is a better solution but yours works perfectly for me and totally solved my issue. Thank you!!!
There is not built in solution for this. ACF only provides a simple shortcode for basic field types. Anything more than this you need to code yourself, as in this case. It looks like a good solution to me.
@elliot Still think this would be a nice addition to the plugin. I know my code isn’t that fancy but if you want to take it from there, I’d be honored 🙂
Your best hope of getting this to E would be here https://www.advancedcustomfields.com/contact/
However, I wouldn’t count on it. There are existing plugins that will help with this type of thing, I’ve never used them but elementor https://elementor.com/ has been mentioned on this forum. ACF itself is focused more on devs that use it by modifying or building custom template files.
I want to display product attributes and add the attributes label before, so i need a conditional to embed the label and shortcode to hide the label if the product has no attribute.
The conditional shortcode consists of an opening and closing shortcode,
The topic ‘Conditional shortcodes’ 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.