在博客当前主题目录下的functions.php内添加如下代码:
- add_action('my_daily_event', 'do_this_daily');
- function my_activation() {
- if ( !wp_next_scheduled( 'my_daily_event' ) ) {
- wp_schedule_event(time(), 'daily', 'my_daily_event');
- }
- }
- add_action('wp', 'my_activation');
- function do_this_daily() {
- $doc = new DOMDocument();
- $doc->load('http://www.xueyueyu.com/feed/'); /*修改为你自己的RSS地址*/
- $newhtml = '';
- foreach ($doc->getElementsByTagName('item') as $node) {
- $link = $node->getElementsByTagName('link')->item(0)->nodeValue;
- $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
- $newhtml .= '<li><a target="_blank" href="'.$link.'">'.$title.'</a></li>';
- }
- /*生成最新文章列表到博客根目录的newposts.htm文件中*/
- $newfile="newposts.htm";
- $file = fopen ($newfile, "w");
- fwrite($file, $newhtml);
- fclose ($file);}
这样,便可以在网站任意地方展示通过一段代码来展示博客最新文章,调用方法与如何从论坛调出html格式的帖子跟帖所述类似。
另外对于所有提供RSS内容的php网站(不限于WP),均可以采用上述PHP代码生成html列表文档。
- <?php
- $doc = new DOMDocument();
- $doc->load('http://www.xueyueyu.com/feed/');
- $newhtml = '';
- foreach ($doc->getElementsByTagName('item') as $node) {
- $link = $node->getElementsByTagName('link')->item(0)->nodeValue;
- $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
- $newhtml .= '<li><a target="_blank" href="'.$link.'">'.$title.'</a></li>';
- }
- /*echo直接返回html列表代码 */
- echo $newhtml;
- /*生成htm文件,可以根据需要选择直接返回html代码或生成htm文件*/
- $newfile="newposts1.htm";
- $file = fopen ($newfile, "w");
- fwrite($file, $newhtml);
- fclose ($file);
- ?>
php直接返回html代码示例:http://www.xueyueyu.com/rss.php
php生成htm文件示例:http://www.xueyueyu.com/newposts1.htm
注:echo返回的htm是实时更新的;生成html需要定期执行php代码才能更新htm文件,方法不统一。
没想到有那么多朋友没有理解,看来文笔还是有点差啊,如果执行过程中有问题的话可以加我的QQ:155381263

