0

I have created few question/ answers using ACF.

Field names are question_1, question_2 and so on.

My codes are:

<?php if (get_field('question_1') == 'yes') && (get_field('question_2') == 'yes'):?>
Answer: Yes
<?php:?>
Answer: No
<?php endif; ?>

How to limit code repetition, if I have 100 questions in number, I just want to avoid using

get_field('question_1') == 'yes' to get_field('question_100') == 'yes'

1 Answer 1

1

U can use get_fields() which return all fields from current page. Additionally you can filter array checking which key matches.

Example:

$questions = get_fields();
$questions_array = array_filter($questions, function($key) {
    return strpos($key, 'question') === 0;
}, ARRAY_FILTER_USE_KEY);

foreach( $questions_array as $question) {
 // your code here
}

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.