Reputation: 3
Im looking for a way to output my wordpress custom fields in this format:
"genre": [
"genre 1",
"genre 2",
"genre 3"
]
using this code:
"genre": [
<?php if( have_rows('genres') ):
while ( have_rows('genres') ) : the_row();
$genre = get_sub_field('genre');
?>
"<?= $genre; ?>",
<?php
endwhile;
endif; ?>
]
But the result (logically) has a comma at the last genre, which creates errors.
"genre": [
"genre 1",
"genre 2",
"genre 3",
]
How can i fix this?:
Upvotes: 0
Views: 59
Reputation: 78994
I don't know Wordpress (and never will) but that's JSON, why not:
while ( have_rows('genres') ) : the_row();
$genres[] = get_sub_field('genre');
endwhile;
echo json_encode(['genre' => $genres]);
Upvotes: 1