getting closer to the solution of my problem... but now I am stuck again.
I am trying return all audio attachments linked to a post. Each post has an .ogg and a .mp3 file. Each file is an array. So separate arrays for the .ogg and .mp3 files. So I return their 'post_parent' value and use this to link them. I use this 'post_parent' as a key in my array. It works, but not like I want it to.
This is what I currently get, it is actually correct...
[18653] => Array (
[0] => Array (
[mp3] => 1.mp3
[ogg] => 1.ogg
)
)
[18612] => Array (
[0] => Array (
[ogg] => 2.ogg
[mp3] => 2.mp3
)
[1] => Array (
[ogg] => 3.ogg
[mp3] => 3.mp3
)
)
[18605] => Array (
[0] => Array (
[ogg] => 4.ogg
[mp3] => 4.mp3
)
)
[18592] => Array (
[0] => Array (
[ogg] => 5.ogg
[mp3] => 5.mp3
)
[1] => Array (
[ogg] => 6.ogg
[mp3] => 7.mp3
)
)
... but my code (at the bottom) can't do this:
[18612] => Array (
[0] => Array (
[ogg] => 12.ogg
[mp3] => 12.mp3
)
[1] => Array (
[ogg] => 13.ogg
[mp3] => 13.mp3
)
[2] => Array (
[ogg] => 14.ogg
[mp3] => 14.mp3
)
[3] => Array (
[ogg] => 15.ogg
[mp3] => 15.mp3
)
)
This is the code I use. It's weird, I know. I'm learning.
My problem is, as you can see, I use the numbers 0 and 1, I just hard coded them in. But it won't work if there are let's say, 4 files in a post (like my sample above). The files after the second found file will replace the $playlist_all[$parent_id][1]. So the [2] and [3] will not be created. And I exactly know why by the way, but I don't know how to make this stuff check if there is an key inside the array, and if so, increment it. I know about for loops and increment stuff... just not sure how it would work with this...
Again, I know where I am going wrong, but this is the only solution I can think of. I just check if there is a [0], if not create it. If there is a [0] I create the [1].
foreach ( $attachments as $attachment ) {
$parent_id = $attachment->post_parent;
$title = $attachment->post_title;
$type = $attachment->post_mime_type;
if ($type == 'audio/ogg') {
if (!$playlist_all[$parent_id][0]['ogg']) {
$playlist_all[$parent_id][0]['ogg'] = $attachment->guid;
} else {
$playlist_all[$parent_id][1]['ogg'] = $attachment->guid;
}
} elseif ($type == 'audio/mpeg') {
if (!$playlist_all[$parent_id][0]['mp3']) {
$playlist_all[$parent_id][0]['mp3'] = $attachment->guid;
} else {
$playlist_all[$parent_id][1]['mp3'] = $attachment->guid;
}
}
}
I hope it's clear, it's messy code, but I got it to work. Well for 50%.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire