php - Assign all custom fields to variables with the same name? -
if have custom fields in wordpress post, there way set custom fields variables of same name automatically?
i.e instead of
$custom_fields = get_post_custom(); if (isset($custom_fields['field_1'][0])) { $field_1 = $custom_fields['field_1'][0]; } if (isset($custom_fields['field_2'][0])) { $field_2 = $custom_fields['field_2'][0]; } etc.....
is there way skip ifs , assign every valid custom field var automatically?
you try this:
$custom_fields = get_post_custom(); foreach($custom_fields $k => $v) { ${$k} = $v[0]; }
it works using variable variables, setting new variable key value, , value 0-th index in array, show in question.
Comments
Post a Comment