Replies: 0
I’m trying to combine a JSON array with a WP_Post Array, but no matter what I do, I either get doubles (most likely due to foreach/while nested loops), or I get the errors: Fatal error: Uncaught Error: Cannot use object of type WP_Post as array and Error: Cannot use object of type WP_Post as array.
I either try:
$args = array(
'post_type' => 'albums',
'order' => 'desc'
);
$album_posts = get_posts($args);
$url = 'https://itunes.apple.com//lookup?id=685783359&entity=album';
$json = file_get_contents($url);
$data = json_decode($json, true);
if($album_posts)
$merge_album_info = array_merge($data['results'], $album_posts);
print_r($merge_album_info);
foreach($merge_album_info as $album) {
if($album['wrapperType'] !== 'artist') {
echo '<div class="col-md-4"><a href="'.esc_url(home_url('/')).'albums/'.$album['post_name'].'"><img src="'.str_replace('100x100', '500x500', $album['artworkUrl100']).'" class="img-fluid"></a></div>';
}
}
Which results in the errors, or:
$args = array(
'post_type' => 'albums',
'order' => 'desc'
);
$albums = get_posts($args);
$url = 'https://itunes.apple.com//lookup?id=685783359&entity=album';
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach($data['results'] as $album) {
if($album['wrapperType'] !== 'artist') {
if($albums){
foreach($albums as $a) {
echo '<div class="col-md-4"><a href="'.esc_url(home_url('/')).'albums/'.$a->post_name.'"><img src="'.str_replace('100x100', '500x500', $album['artworkUrl100']).'" class="img-fluid"></a></div>';
}
}
}
insert_new_album($album['collectionName'], $album['collectionId']);
}
Which results in the doubles.
I’m parsing JSON information to display a list of albums. With that I have it create a post, passing the album name as well as the album ID. When someone clicks on the album (which has JSON álbum artwork information as well as the post slug), they are taken to a page that displays the album’s track information. So it doesn’t create a post with ALL the information (one field per track listing) I have it both reading from the post and JSON (in this page and another).
Just trying to figure out the best way to do this. Any suggestions would be greatly appreciated. Thanks in advance!
- This topic was modified 12 minutes ago by thelos999.