2015-09-23 50 views
0

我有一些代码,将通过十个最新的WordPress博客帖子通过我的非WordPress的网站。有没有办法显示分配给博客文章的精选图片?我已成功回复博客标题,日期和正文。喂养WordPress的博客条目到自己的网站

 <?php 
     global $text, $maxchar, $end; 
     function substrwords($text, $maxchar, $end='...') { 
      if (strlen($text) > $maxchar || $text == '') { 
      $words = preg_split('/\s/', $text);  
      $output = ''; 
      $i  = 0; 
      while (1) { 
       $length = strlen($output)+strlen($words[$i]); 
       if ($length > $maxchar) { 
       break; 
       } else { 
       $output .= " " . $words[$i]; 
       ++$i; 
       } 
      } 
      $output .= $end; 
      } else { 
      $output = $text; 
      } 
      return $output; 
     } 

     $rss = new DOMDocument(); 
     $rss->load('http://myblog.wordpress.com/rss/'); // <-- Change feed to your site 
     $feed = array(); 
     foreach ($rss->getElementsByTagName('item') as $node) { 
      $item = array ( 
      'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
      'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 
      'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 
      'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, 
     ); 
      array_push($feed, $item); 
     } 

     $limit = 10; // <-- Change the number of posts shown 
     for ($x=0; $x<$limit; $x++) { 
      $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']); 
      $link = $feed[$x]['link']; 
      $description = $feed[$x]['desc']; 
      $description = substrwords($description, 400); 
      $date = date('l F d, Y', strtotime($feed[$x]['date'])); 
      echo '<div style="margin-bottom:25px;">'; 
      echo '<h3><strong><a style="color: #139035;" href="'.$link.'" title="'.$title.'" target="_blank">'.$title.'</a></strong></h3>'; 
      echo '<p><small><em>Posted on '.$date.'</em></small></p>'; 
      echo '<p>'.$description.'</p>'; 
      echo '<span> <strong><a target="_blank" style="color: #139035;" href="'.$link.'" title="'.$title.'">Read blog ></span></strong>'; 
      echo '</div>'; 
     } 
     ?> 
+0

WordPress的默认RSS供稿不会暴露特色照片。您需要在博客上扩展RSS源的功能。 – rnevius

回答

0

您可以使用下面的代码添加特色图片到你的RSS

function img_in_rss($content) { 
global $post; 
if (has_post_thumbnail($post->ID)){ 
$content = get_the_post_thumbnail($post->ID, 'medium') . $content; 
} 
return $content; 
} 

add_filter('the_excerpt_rss', 'img_in_rss'); 
add_filter('the_content_feed', 'img_in_rss'); 
+0

在底部添加过滤器位给我一个错误 – rans

+0

你得到什么错误?我测试了它将特色图片添加到Feed中。 – Mohsin

+0

我正在使用CakePHP,我得到的错误是'致命错误 错误:调用未定义的函数add_filter()\t'..你在哪里插入这段代码 – rans