0

I have created a 'related posts' custom field and would like to loop through all the posts that are added as a relation and show a bit of info from each (the title, a link and the featured image). I've got the title and permalink coming through nicely, but for some reason I can't get the related posts featured image to appear. I'm hoping someone can point me in the right direction?

Here's the code I'm using to create an array of all the post info:

$relatedPosts = get_custom_field('relatedposts:to_array', 'get_post');

foreach ($relatedPosts as $p) {
  $postTitle = $p['post_title'];    
  $postID = $p['ID'];
  $postPermalink = $p['guid'];
  $postTNID = $p['thumbnail_id]'];

  $post_html[] = '<div class="relPost"><a href="' . $postPermalink . '" title="' . $postTitle . '">' . $postTitle . '</a> '. wp_get_attachment_image($postTNID). ' </div>';

}//end foreach

Then I use the following to show it on the page:

if ($relatedPosts != "") { 
echo implode("\n", $post_html);
}

Everything except the image works...

0

Looks like a simple typo:
$p['thumbnail_id]'];
should be:
$p['thumbnail_id'];

  • D'oh. That was driving me spare! Thanks for taking the trouble to point out my error - that fixed it a treat! – Kev May 28 '14 at 19:02
  • @Kev you should set debug to true in wp-config when you are developing themes/plugins. This would have been easily spotted, and saved you lot of time and frustration – Pieter Goosen May 29 '14 at 4:34
  • @PieterGoosen - thanks very much, I didn't know that was possible in Wordpress so thanks for the info. That's my lesson for today :) – Kev May 29 '14 at 8:19
  • @Kev my pleasure. I also learned the hard way first :) – Pieter Goosen May 29 '14 at 8:30

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.