• This is probably PHP coding 101, but after an hours-long lengthy trawl through the various search engines & YouTube, it appears I’m the first and only person to ask this simple question – hoping someone can answer in a non-coder friendly way.

    Working with ACF, Elementor (I know) and Woo Commerce as a way to feature additional product variables. Ideally “true” would return something other than “1”, like “Is this product heavy” = “Yes”. Happy with “false” not displaying a result.

    I can’t for the life of me find a code example that relates to my simplistic query. Not even in the code examples provided by ACF.

    Using dynamic fields in Elementor to show the ACF data on a Single Product Template. Would apply the PHP using Code Snippets plugin.

    Can someone point me in the right direction as to how I can achieve this? Forever grateful. PS: I get the high-level logic behind “get field > if true > print XYZ”, but no concept as to how I can actually code this.

    Have tried to cobble together a few PHP entries. Each time it either a) doesn’t do anything b) causes a critical site error

Viewing 1 replies (of 1 total)
  • Hello @sywy,

    Create a shortcode that fetches 0/1 values from an SCF field and outputs ‘True’ or ‘False.’ You can add the following code to your theme’s functions.php file or use the Code Snippets plugin.

    This shortcode will display ‘True’ if the field value is 1 and ‘False’ if it’s anything else.”

    function scf_true_false_shortcode($atts) {
    // Extract shortcode attributes for flexibility
    $atts = shortcode_atts(
    array(
    'field_name' => '', // Set your SCF field name
    'post_id' => 5 // Replace the post ID of the post you will add the shortcode to
    ),
    $atts
    );

    // Get the SCF field value
    $value = get_post_meta($atts['post_id'], $atts['field_name'], true);

    // Return True/False based on value
    return $value == 1 ? 'True' : 'False';
    }

    // Register the shortcode
    add_shortcode('scf_true_false', 'scf_true_false_shortcode');

    If you create a SCF filed for display only on posts

    Add the shortcode [scf_true_false field_name="your_field_name"] on any post.

    Replace "your_field_name" with the actual SCF field name you want to retrieve.

    Replace the post id with the actual post id you will add the shortcode to

    Save the post and check on the frontend, if the value of the SCF field is 1 the front end will disply True otherwise False, try changing the value and saving the post to check if it works properly

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.