Well, structuring is partly a matter of taste, but it in this case it seems to be a good idea to include all the functions in a separate file. That includes the code that you would have in both template files. And rather than the chain of elseif
you could use PHP’s switch statement. So you would have:
In your functions.php
(just plain, not depending on a hook)
require_once (get_template_directory() . '/field-functions.php');
In your template files
deal_with_fields ();
In your field-functions.php
function deal_with_fields () {
$all_fields = get_fields ();
foreach ($all_fields as $field) {
switch ($field) {
case 'field_1' : deal_with_field_1 ($field); break;
case 'field_2' : deal_with_field_2 ($field); break;
....
}
}
}
function deal_with_field_1 ($field) {
do your thing;
}
function deal_with_field_2 ($field) ....