1

I'm not sure why the foreach loop is still printing an array for my $education variable. This code is supposed to get the custom post fields. It works with the title, the image and the about variable, but not when the variable has been put in an array.

<?php $args = array( 'post_type' => 'team members', 'posts_per_page' => 40 , 'paged' => 1,);
  $loop = new WP_Query( $args );
  $counter = 1;

  while ( $loop->have_posts() ) : $loop->the_post();

      $title = get_the_title();
      $about = get_post_custom_values('about');
      $education = get_post_custom_values('education');
      $certications = get_post_custom_values('certications');
      $image_upload = get_post_meta($post->ID, 'profile_image', true); // CALL IMAGE
        echo '<div class="tab blue black '. join( ' ', get_post_class() ) .'"><input id="inputnumber' . $counter . '" type="checkbox" name="group1" class="trigger"><label for="inputnumber' . $counter . '">' . $title . '</label> <span class="content">' . $about[0] . '</span></div>';
      foreach($education as $value) {
      echo $value; // Displays "12" (there are 12 tracks on this album)
  }
    echo wp_get_attachment_image($image_upload);  // Echo image

    endwhile;

  ?>

UPDATE: vardump prints the number of populated rows, but not the actual content of these rows. The custom field I am trying to call is a small table with content.

  • echo "$value"; I think echo $value; – Andrea Somovigo Jul 30 '18 at 17:15
  • I'm afraid that didn't do the trick, but I think you're right about it needing no quotes - thanks. EDIT: updated above code – Remco Jul 30 '18 at 17:23
  • Try foreach($education as $key => $value) so you're not echoing the keys. – WebElaine Jul 30 '18 at 19:23
  • still nothing - just the array numbers. :( – Remco Jul 30 '18 at 20:41
  • 1
    Try: foreach($education as $key => $value){echo $value }; possibly do a var_dump($education) before the cycle, to see exactly what's in there – Andrea Somovigo Jul 30 '18 at 22:34
0

OK, so this is what I did wrong.

This was an array within an array, which meant that I had to use the element index to echo the values. I'm not even sure I'm explaining it right, but thanks for your help all. It's working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.