缩略图未显示在博客中

时间:2013-07-05 作者:Marc

我正在尝试向wordpress博客(由其他人创建)添加缩略图。作为Wordpress的新手,我知道我需要添加“the\\u post\\u thumbnail();”在“循环”内部,但查看索引。php文件我没有看到有循环。

如果您查看代码,可以看到我尝试插入了the\\u post\\u缩略图();但看看前端http://www.chcdev.co.uk/blog/ 仍然没有缩略图。(是的,我也在帖子中添加了一幅特色图片)。你们这些聪明的人能解释一下吗?

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

1 个回复
SO网友:Rarst

WP中的循环术语指的是在post对象上迭代的概念,并且(通常)用当前对象的数据填充全局变量。

可通过以下方式之一进行识别:

  • the_post() 函数调用(主循环)$some_variable->the_post() 方法调用(使用WP_Query 对象)
  • setup_postdata( $post ) (更原始的版本,使用post对象数组)
在您的情况下,这不会真正发生,而且会有问题,因为不是在post集合上迭代($posts = get_posts() 它们的数据被完全分离并重写为自定义数组,最终$news 通过它,您的输出循环。

实际上,这其中的大部分都必须重写,以将其敲入正常的WP循环。

阻力最小的路径可能是:

商店$p->ID 处理其他数据时,请使用get_the_post_thumbnail() 函数,它接受post要处理的ID参数,而不是依赖全局变量

结束

相关推荐

2 loops in blog homepage

I would like to have to query loops running on my blog homepage:Featured posts on top displaying 3 posts from the category : \"Featured\"Underneath it just the basic loop of wordpress which display the latest (10 or other, whatever is selected within the