Using a php statement within a echo do_shortcode() function (WordPress)

I’ve been looking for a solution to this, but can’t see to the right way to implement this.

Here is the page I am working on:

http://indiaelder.sites.uofmhosting.net/health-topics/alzheimers-disease/

As you can see, this is a placeholder for a very early medical page. I have a tabbed menu that list (with some filler text) a bunch of information about Alzheimer’s disease, organized by categories (symptoms, causes, etc).

This is achieved in my code by doing the following:

<?php
/*This gets the shortcode code to open the tabs */
                echo do_shortcode('
                [su_tabs vertical="yes"]
                    [su_tab title="Symptoms"][acf field="symptoms"][/su_tab]
                    [su_tab title="Causes"][acf field="causes"][/su_tab]
                    [su_tab title="Prognosis"][acf field="prognosis"][/su_tab]
                    [su_tab title="Diagnosis"][acf field="diagnosis"][/su_tab]
                    [su_tab title="Treatment"][acf field="treatment"][/su_tab]
                    [su_tab title="Prevention"][acf field="prevention"][/su_tab]
                [/su_tabs]
                ');
                ?>

I would like to make it so that if a user does not enter any information in one of these fields in the backend, that the corresponding line will not appear.

So if I had a variable named $symptoms, and I set that equal to the value of the field, I was thinking it’d be something like

$symptoms = get_field('symptoms');

And run a conditional to see if it is not ”, and then not echo the corresponding line (in this example, [su_tab title=”Symptoms”][acf field=”symptoms”][/su_tab] would not be included in the echo statement).

I’ve had a great deal of challenge so far in doing this. My first thought was to break the shortcode into parts, but this seems to break the functionality, so I think it all has to be echoed at once. I feel like their must be a way to do this, and I’ve seen some examples but nothing seems to apply to my particular case. Does anyone have any thoughts on how I might do this?


Source: shortcode

Leave a Reply