Trying to display a Wordpress custom field meta_key value.
The data is saved as a WordPress custom field meta_key value
$data = get_post_meta( get_the_ID(), 'data', false);
print_r($data);
Array (
[0] => [
{
"title":"Title A",
"date":"2024-06-06",
"venue":"Venue X"
},
{
"title":"Title B",
"date":"2010-08-08",
"venue":"Venue Y"
}
]
)
The code I am using
<?php
$data = get_post_meta( get_the_ID(), 'data', false);
$size = count($data[0]);
$i = 0;
for( $j = 0; $j < $size; $j++ )
{
?>
<p>Title: <?php echo $data[$i][$j]['title']; ?></p>
<p>Date: <?php echo $data[$i][$j]['date']; ?></p>
<p>Venue: <?php echo $data[$i][$j]['venue']; ?></p>
<?php
}
?>
The output I am getting
Title: [
Date: [
Venue: [
Any pointers on where its going wrong.
To get the data displayed, you need to:
get_post_meta()function totrueinstead offalse,json_decode()function on it (as it's JSON encoded),foreachloop, instead of aforloop.Try the following:
It should work.