0

I'm creating a Run Report page for a ski resort. I have created an ACF group calls Runs. Within this ACF group there are a number of ski runs that use the checkbox field with the values 'Day' 'Night' and 'Snowmaking'. This function is triggered by a shortcode where the Run name is passed via an attribute. Here is the code:

add_shortcode('show_run', 'get_run_status');

function get_run_status($atts) {
    $atts = shortcode_atts(array(
        'run' => '',
    ), $atts);
    $field_name = sanitize_text_field($atts['run']);
    $field_group_key = 'group_65d7bdff9d143';
    $fields = acf_get_fields($field_group_key);
    $html_output = '';
    $found_day = false;
    $found_night = false;
    foreach ($fields as $field) {
        if ($field['name'] === $field_name) {
            $field_values = get_field($field['name']);
            foreach ($field_values as $value) {
                if (strpos($value, 'Snowmaking') !== false) {
                    $html_output = '<img src="/wp-content/uploads/2024/03/Snowflake.png" alt="Snowmaking" />' . $html_output;
                } elseif (strpos($value, 'Day') !== false) {
                    $html_output .= '<img src="/wp-content/uploads/2024/03/Circle-Checkmark.png" alt="Open Day" />';
                    $found_day = true;
                } elseif (strpos($value, 'Night') !== false) {
                    $html_output .= '<img src="/wp-content/uploads/2024/03/Circle-Checkmark.png" alt="Open Night" />';
                    $found_night = true;
                }
            }
            if (!$found_day) {
                $html_output .= '<img src="/wp-content/uploads/2024/03/Red-X.png" alt="Closed" />';
            }
            if (!$found_night) {
                $html_output .= '<img src="/wp-content/uploads/2024/03/Red-X.png" alt="Closed" />';
            }
            $html_output = '<div class="run-row"><div class="run-name">' . esc_html($field_name) . '</div>' . $html_output . '</div>';
            return $html_output;
        }
    }
    return 'No relevant status found for run: ' . esc_html($field_name);
}

This code (while greatly inefficient; for example defining two diff images twice) works. The function loops through all fields that belong to the defined $field_group_key. If the field name matches the shortcode attribute, another loop starts where it breaks down the checkbox values into an array. If 'day', 'night' or 'snowmaking' are found in the values, then specific icon images are displayed as well as the run name in the HTML output.

It later occurred to me that each run needs another field called 'difficulty'. These cant be checkboxes so I deleted the ACF field group and created a new one based on the 'Group' field type. This will allow me to create several fields per run. The hierarchy of the new field group looks like this:

Main Group [all runs]
  Sub Group [specific run]
    Status [checkbox field
    Difficulty [radio field]

My thought was just to add a new loop inside the existing code as follows:

add_shortcode('show_runs', 'get_run_status');

function get_run_status($atts) {
    $atts = shortcode_atts(array(
        'run' => '',
    ), $atts);
    $short_name = sanitize_text_field($atts['run']);
    $group_key = 'group_65e26ad7b9a44';
    $main_fields = acf_get_fields($group_key);
    $html_output = '';
    $found_day = false;
    $found_night = false;
    foreach ($main_fields as $main_field) {
        if ($main_field['name'] === $short_name) {
            $sub_fields = acf_get_fields($main_field);
            foreach ($sub_fields as $sub_field) {
                if ($sub_field['name'] === 'status') {
                    $field_values = get_field($sub_field['name']);
                    foreach ($field_values as $value) {
                        if (strpos($value, 'Snowmaking') !== false) {
                            $html_output = '<img src="/Snowflake.png" alt="Snowmaking" />' . $html_output;
                        } elseif (strpos($value, 'Day') !== false) {
                            $html_output .= '<img src="/Circle-Checkmark.png" alt="Open Day" />';
                            $found_day = true;
                        } elseif (strpos($value, 'Night') !== false) {
                            $html_output .= '<img src="/Circle-Checkmark.png" alt="Open Night" />';
                            $found_night = true;
                        }
                    }
                    if (!$found_day) {
                        $html_output .= '<img src="/Red-X.png" alt="Closed" />';
                    }
                    if (!$found_night) {
                        $html_output .= '<img src="/Red-X.png" alt="Closed" />';
                    }
                    $html_output = '<div class="run-row"><div class="run-name">' . esc_html($short_name) . '</div>' . $html_output . '</div>';
                    return $html_output;
                }
            }
        }
    }
    return 'No relevant status found for run: ' . esc_html($short_name);
}

My thought was define the sub group the same way I defined the main group, using acf_get_fields(). I've optimized the variable names in the second example for easier reading. Bottom line, it doesn't work and I haven't found the solution. If the new code can be made to work then I can integrate the Difficulty field.

The radio values for difficulty are 'easy', 'intermediate' and 'hard'. Whichever radio option is selected, the HTML output will display easy.png interm.png hard.png etc. My first priority is just to get the first part of the code working, i'll work on difficulty following though if you have a suggestion on the second part, you have my thanks.

The final HTML output will look like this:

<div class="run-row">
  <img src="easy.png"> // if run was set to easy
  <div class="run-name">Garfield</div> // run name passed via shortcode
  <img src="Snowflake.png"> // if snowmaking was checked
  <img src="Circle-Checkmark.png"> // if Day was checked
  <img src="Red-X.png"> // if Night was unchecked
</div>

Again, the first code example works. When I changed to a group within a group and added the second loop it fails. Your suggestions are greatly appreciated.

P.S. Ai was no help. Checked before posting.

1 Answer 1

1

I should not have been looping at all. This (not optimized) code fixes all issues and adds Difficulty.

add_shortcode('get_run', 'get_run_status');

function get_run_status($atts) {
    $atts = shortcode_atts(array(
        'run' => '',
    ), $atts);
    $run_output = '';
    $diff_output = '';
    $found_day = false;
    $found_night = false;
    $run_atts = sanitize_text_field($atts['run']);
    $run_query = get_field($run_atts);
    if($run_query):
        $run_status = $run_query['status'];
        $run_difficulty = $run_query['difficulty'];
    endif;
    foreach ($run_status as $status_value) {
        if (strpos($status_value, 'Snowmaking') !== false) {
            $html_output = '<img src="/wp-content/uploads/2024/03/Snowflake.png" alt="Snowmaking" />' . $html_output;
        } elseif (strpos($status_value, 'Day') !== false) {
            $html_output .= '<img src="/wp-content/uploads/2024/03/Circle-Checkmark.png" alt="Open Day" />';
            $found_day = true;
        } elseif (strpos($status_value, 'Night') !== false) {
            $html_output .= '<img src="/wp-content/uploads/2024/03/Circle-Checkmark.png" alt="Open Night" />';
            $found_night = true;
        }
    }
    if ($run_difficulty === 'Easiest') {
        $diff_output = '<img src="/wp-content/uploads/2024/03/Green-Circle.png" alt="Easiest" />';
    } elseif ($run_difficulty === 'Intermediate') {
        $diff_output .= '<img src="/wp-content/uploads/2024/03/Blue-Square.png" alt="Difficult" />';
    } elseif ($run_difficulty === 'More Difficult') {
        $diff_output .= '<img src="/wp-content/uploads/2024/03/Black-Diamond.png" alt="More Difficult" />';
    } elseif ($run_difficulty === 'Most Difficult') {
        $diff_output .= '<img src="/wp-content/uploads/2024/03/Double-Black.png" alt="Most Difficult" />';
    } elseif ($run_difficulty === 'Freestyle Terrain') {
        $diff_output .= '<img src="/wp-content/uploads/2024/03/M-Circle.png" alt="Freestyle Terrain" />';
    }
    if (!$found_day) {
        $html_output .= '<img src="/wp-content/uploads/2024/03/Red-X.png" alt="Closed" />';
    }
    if (!$found_night) {
        $html_output .= '<img src="/wp-content/uploads/2024/03/Red-X.png" alt="Closed" />';
    }
    $html_output = '<div class="run-row">' . $diff_output . '<div class="run-name">' . esc_html($run_atts) . '</div>' . $html_output . '</div>';
    return $html_output;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.