Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add thumbnails to a wordpress blog (that was created by someone else). As a newcomer to Wordpress I understand I need to add "the_post_thumbnail();" inside "the loop" but looking at the index.php file I dont see that there is a loop.

If you look at the code you can see i have tried to insert the the_post_thumbnail(); but looking at the front end http://www.chcdev.co.uk/blog/ there are still no thumbnails. (Yes I added a featured image to the posts aswell). Can any of you clever people shed any light on this?

<div id="main" class="grid_11">


<?php

// Settings
$blog_ids    = array(5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,28);
$news_limit  = 15;
$excerptsize = 250;
$pipes_url   = 'http://pipes.yahoo.com/pipes/pipe.run?   _id=ffc88cc14e18d87eb77c214c27f84e02&_render=json';

// Fetch recent posts for each blog and add them to an array indexed by date
foreach ($blog_ids as $b) {
switch_to_blog($b);
$posts = get_posts('&numberposts='.$news_limit.'&order=DESC&orderby=post_date');
foreach ($posts as $p) {
    $news_tmp[strtotime($p->post_date_gmt)]['permalink'] = get_permalink($p->ID);
    $news_tmp[strtotime($p->post_date_gmt)]['date']      = date("j", strtotime($p->post_date_gmt));
    $news_tmp[strtotime($p->post_date_gmt)]['month']     = date("M", strtotime($p->post_date_gmt));
    $author_arr  = get_userdata($p->post_author);
    $news_tmp[strtotime($p->post_date_gmt)]['author']    = $author_arr->display_name;
    $news_tmp[strtotime($p->post_date_gmt)]['title']     = $p->post_title;
    $news_tmp[strtotime($p->post_date_gmt)]['content']   = preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', strip_tags($p->post_content, '<p>'));
    $news_tmp[strtotime($p->post_date_gmt)]['excerpt']   = '<p>' . substr( $news_tmp[strtotime($p->post_date_gmt)]['content'], 0, strrpos( substr( $news_tmp[strtotime($p->post_date_gmt)]['content'], 0, $excerptsize), ' ' ) ) . '...</p>';
}
}

// Fetch Yahoo pipes data and add them to an array indexed by date
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pipes_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data  = curl_exec($ch);
curl_close($ch);
$pipes = json_decode($data, true);
$c=1;
foreach ($pipes['value']['items'] as $pipe) {
$d_parts = explode(' ', $pipe['pubDate']);
$t_parts = explode(':', $d_parts[4]);
$d = $d_parts[1]." ".$d_parts[2]." ".$d_parts[3]." ".$d_parts[4];
$news_tmp[strtotime($d)]['permalink'] = $pipe['link'];
$news_tmp[strtotime($d)]['date']      = ltrim($d_parts[1], '0');
$news_tmp[strtotime($d)]['month']     = $d_parts[2];
$news_tmp[strtotime($d)]['author']    = 'Yahoo!';
$news_tmp[strtotime($d)]['title']     = $pipe['title'];
$c++;
if ($c == $news_limit) {break;}
}

// Sort by the index (date)
ksort($news_tmp);

// Trim array to requested number of posts
$news = array_slice(array_reverse($news_tmp), 0, $news_limit);

// Display

foreach ($news as $n) {

echo "          <div class=\"newspost\">\n";
echo "              <h2><a href=\"".htmlentities($n['permalink'])."\"     title=\"Permalink to ".$n['title']."\" target=\"_blank\">".$n['title']."</a></h2>\n";

echo "<div class=\"thumbnailmt\">\n";    
 the_post_thumbnail(); 
echo "          </div>\n";

if( $n['author'] != 'Yahoo!') {
    echo "              <span class=\"author\">Written by: ".$n['author']." on " . $n['month'] . " " . $n['date'] . "</span>\n";
}
else{
    echo "              <span class=\"author\">Generated by Yahoo!     Pipes. Written on " . $n['month'] . " " . $n['date'] . "</span>\n";
}
echo "              <div class=\"main-divider\"></div>\n";
echo "              " . $n['excerpt'] . "\n";
echo "          </div>\n";
echo "          <!-- END NEWS ITEM -->\n";


}

switch_to_blog(19);
?>


</div><!-- END MAIN -->
share|improve this question
add comment

1 Answer

The loop term in WP refers to concept of iterating over post objects and (typically) filling global variables with data of current one.

It can be identified by one of:

  • the_post() function calls (main loop)
  • $some_variable->the_post() method calls (secondary loops using WP_Query object)
  • setup_postdata( $post ) (more raw version, working with array of post objects)

In your case this doesn't really happen and would be problematic, because rather than iterating on set of posts ($posts = get_posts() their data is completely torn apart and rewritten into custom arrays which end up in $news through which your output loops.

Really most of this would have to be rewritten to hammer it into normal WP loop.

The path of least resistance would probably be:

  1. store $p->ID with other data, when processing it
  2. use get_the_post_thumbnail() function, which accepts ID argument for post to work on, instead of relying on global variable
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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