I'm Trying to make a function, my code works but i wonder is there any way to simplify my code? with loop maybe?
Background:
I try to show data from usermeta (the usermeta from plugin) so these are the meta key:
- child_name
- child_name_1
- child_name_2
- child_name_3
and so on.. based on how much user input their children in database.
example, if user only input 2 children, meta key will be:
- child_name
- child_name_1
temporary, i limit only 3 children to input, because i can't simplify it.
function get_child_name(){
$user = wp_get_current_user();
$count_extra_field = $user->wppb_repeater_field_15_extra_groups_count;
$count_child = $count_extra_field + 1;
$child_name_0 = $user->child_name;
$child_name_1 = $user->child_name_1;
$child_name_2 = $user->child_name_2;
$child_names = array($child_name_0,$child_name_1,$child_name_2);
$n=0;
foreach($child_names as $child_name) {
if($n==$count_child) break;
$n++;
echo $child_name;
}
}
add_shortcode('child_names', 'get_child_name');
Above code is works, as I use it now. but is it possible to simplify it / automate the process? So, user can input as many children as possible. or if I want to increase the limit (for input children names) I don't have to add the code again manually, is it possible?