The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Showing only existing content from variables

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #198250

    I 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!

    Ilan Firsov

    Start with a print_r or a var_dump to see what the array looks like when filled.
    Does it have empty items? null items? – use array_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 to array_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 );

    This 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++){

    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 :)

    Ilan Firsov

    If 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'] . '">';
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.