- This topic is empty.
-
AuthorPosts
-
March 15, 2015 at 4:04 pm #198250
Thadley
ParticipantI have custom fields set up for my work pages that will display up to 4 images inside each individual work page from within wordpress. I have 4 fields and not all of them will be filled for each project.
I created variables that contain the image url by using the specific custom field function ( get_field(‘whatever’); )and inserted those variables into an array, $array.
I made a for loop to loop through the array and echo out each array item ( image url paths ) into an img tag where the source for the image is the $array[$i][‘url’].
I got them to show up fine but the problem i’m having is how can I display only those array items that have a filled out field.
I am not sure what the if statement’s condition needs to be. If only 2 out of the 4 fields are filled out inside wordpress than 2 images show up which is perfect but then the last 2 images containers also show up with no image because the field itself is empty. Does that make sense?
Guidance would be sweet!
March 16, 2015 at 12:10 am #198262Ilan Firsov
ParticipantStart with a
print_r
or avar_dump
to see what the array looks like when filled.
Does it have empty items? null items? – usearray_filter
, it will remove all of them:$result = array_filter( $array );
If you need more complex way of removing items, let’s say you get the custom field data but only the
url
part is empty, you can pass a callback function toarray_filter
and check by that:$result = array_filter( $array, function( $v ) { if( empty( $v['url'] ) ) { return false; } return true; // or return !empty( $v['url'] ); // the value will be added to the $result array only if you return true } );
Note if you use numeric indexes you might have to re-index the array:
$result = array_values( $result );
March 17, 2015 at 1:59 pm #198408Thadley
ParticipantThis worked :)
I filtered the array with array_filter(). I made another variable that contains the length of the array and i used that variable inside my for loop.
for($i=0; $i=$length-1; $i++){ //code }
It works and I just have to make sure the images are filled out in order. ex: 1,2,3 are filled and not 1,2,4 otherwise it will have a blank box.
Thanks for the guidance sir :)
March 18, 2015 at 12:52 am #198433Ilan Firsov
ParticipantIf you re-index the array with
array_values
it can “fix” this problem.
This will turn an array like this (after removing elements):[ 1 => ['url' => '...', 'title' => '...'], 3 => ['url' => '...', 'title' => '...'] ]
to this:
[ 0 => ['url' => '...', 'title' => '...'], 1 => ['url' => '...', 'title' => '...'] ]
Also you can use a
foreach
loop to iterate over the array, this way you don’t need the index:foreach($result_array as $item) { // $item will be used to reference the array item // this is the same as using $result_array[$i] in a for loop echo '<img src="' . $item['url'] . '" title="' . $item['title'] . '">'; }
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.