如何从WP的RSS中调出html格式的帖子

      很多网站在一级目录下放置博客并提供RSS内容订阅,如在www.example.com/根目录下放置主站,在www.example.com/blog/下放置博客,本文分享“如何将WordPress中的RSS生成html列表以供根目录下的网站调用”起到根目录内容实时更新的作用(参见:如何从论坛调出html格式的帖子)。以www.xueyueyu.com为例,其RSS地址为http://www.xueyueyu.com/feed/,现在我们要把这个RSS内容处理生成html格式的列表并每天更新至newposts.htm(博客目录下)。实现方法如下:
在博客当前主题目录下的functions.php内添加如下代码:

  1. add_action('my_daily_event', 'do_this_daily');
  2. function my_activation() {
  3.         if ( !wp_next_scheduled( 'my_daily_event' ) ) {
  4.                 wp_schedule_event(time(), 'daily', 'my_daily_event');
  5.         }
  6. }
  7. add_action('wp', 'my_activation');
  8. function do_this_daily() {
  9.   $doc = new DOMDocument();
  10.   $doc->load('http://www.xueyueyu.com/feed/'); /*修改为你自己的RSS地址*/
  11.   $newhtml = '';
  12.   foreach ($doc->getElementsByTagName('item') as $node) {
  13.           $link = $node->getElementsByTagName('link')->item(0)->nodeValue;
  14.           $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
  15.           $newhtml .= '<li><a target="_blank" href="'.$link.'">'.$title.'</a></li>';
  16.   }
  17. /*生成最新文章列表到博客根目录的newposts.htm文件中*/
  18. $newfile="newposts.htm";
  19. $file = fopen ($newfile, "w");
  20. fwrite($file, $newhtml);
  21. fclose ($file);}
本文链接:https://www.dnwfb.com/497.html,转载请注明出处。
0

评论0

没有账号? 注册  忘记密码?