/ 博客 / 4366浏览

调用其它站点RSS文章显示在自己的WordPress网站页面上

首先,本篇参考了知更鸟的文章《调用其它站点文章显示在自己的WordPress网站上》。原文如下:

如果在自己WordPress网站上显示其它站点的最新文章是不是很好玩,利用wordpress2.8以上版本新增加的一个fetch_feed函数就可轻松实现.

在添加以下代码之前,应首先在网站根目录新建一个名为 cache 权限为777 的文件夹,作为缓存文件夹.然后在模板(一般是侧边栏sidebar.php模板)适当位置加入以下代码:

<?php
require_once (ABSPATH . WPINC . '/class-feed.php');
$feed = new SimplePie();
$feed->set_feed_url(array('http://zmingcx.com/feed', 'http://themes.wopus.org/feed')); //可以添加多个站点feed地址
$feed->enable_order_by_date(false);
$feed->set_cache_location($_SERVER['DOCUMENT_ROOT'] . '/cache');//缓存文件夹
$feed->init();
$feed->handle_content_type();
?>
<ul>
<?php foreach ($feed->get_items(0,10) as$item)://10是文章篇数 ?>
<li>
<a href="<?php echo $item->get_permalink()?>" rel="bookmark" title="阅读> <?php echo $item->get_title()?>"><?php echo$item->get_title()?></a><!-- 文章标题 -->
<br/><?php echosubstr($item->get_description(),0,180);?><!-- 文章内容 -->
<?php endforeach; ?>
</li>
</ul>

为了方便使用 HotNewspro2.23 版主题的朋友实现此功能,已作好替换文件,供大家下载:

替换文件: feed.zip (1.7 KB, 146 次下载)

将下载的文件分别上传替换原主题目录中,并在style.css中加入:

.rss ul li {
width:209px;
white-space:nowrap;
overflow:hidden;
line-height:23px;
}

注:以上样式也可以不加,使用前可以打开rss.php修改标题及替换调用的feed地址,具体效果可以看本站侧边的"最新主题下载"

但是在实际应用中发现,样式还可以更美观,另外其代码主旨放到侧边栏也不是经常用的,更多的情况应该是放在某个页面或者某篇文章处,因此修改如下:

1、首先在网站根目录新建一个名为 cache 权限为777 的文件夹,作为缓存文件夹。

2、创建模版,可以利用知更鸟HotNewsPro主题文件夹下的几个模版更改。在模版中加入知更鸟上面的代码,但要订正两个地方:

//此处把知更鸟简单的substr函数改为自定义的可以识别中文字的函数cut_strr,就不会乱码了。  
function cut_strr($str,$len) {  
if (strlen($str) <= $len) return $str;  
$n = 0;  
$tempstr = '';  
for ($i=0; $i<$len; $i++) {  
if (ord(substr($str,$n,1)) > 224) {  
$tempstr .= substr($str,$n,3);  
$n += 3;  
$i++; //把一个中文按两个英文的长度计算  
} elseif (ord(substr($str,$n,1)) > 192) {  
$tempstr .= substr($str,$n,2);  
$n += 2;  
$i++; //把一个中文按两个英文的长度计算  
} else {  
$tempstr .= substr($str,$n,1);  
$n ++;  
}  
}  
//此处增加strip_tags(),去掉字串中包含 HTML 及 PHP 的标记,返回字符串形式的值,避免输出值中有分段现象造成各种难看  
$tempstr = strip_tags($tempstr);  
return $tempstr.'...';  
}

以及:

//此处把知更鸟的<br/><?php echosubstr($item->get_description(),0,180);?><!-- 文章内容 --> 改为cut_strr函数  
<br/><div style="margin-left: 20px; "><?php echo cut_strr($item->get_description(),360);?>

3、或者如果你比较懒,直接拷贝本文最下面的代码,在主题中创建一个模版php页面,然后写文时使用模版即可。

<?php  
/*
Template Name: 输出RSS页面  
*/ 
?>  
 
<?php get_header(); ?>  
<div id="content">  
    <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>   
    <!-- menu -->  
    <div id="map">  
        <div class="browse">现在位置 ><a title="返回首页" href="<?php echo get_settings('Home'); ?>/">首页</a> ><?php the_title(); ?></div>  
        <div id="feed"><a href="<?php echo get_option('swt_rsssub'); ?>" title="RSS">RSS</a></div>  
    </div>  
    <!-- end: menu -->  
    <!-- entry -->  
    <div class="clear"></div>  
    <div class="entry_box_s">  
 
                <div class="entry_title_box">  
                <!-- 分类图标 -->  
                <div class="ico"><?php include('includes/cat_ico.php'); ?></div>  
                <!-- end: 分类图标 -->  
                <div class="entry_title"><?php the_title(); ?></div>  
                <div class="archive_info">  
                    <span class="date"><?php the_time('Y年m月d日') ?></span>  
                    <?php include('includes/source.php'); ?>  
                    <span class="comment"> &#8260; <?php comments_popup_link('暂无评论', '评论数 1', '评论数 %'); ?></span>  
                    <?php if(function_exists('the_views')) { print ' &#8260; 被围观 '; the_views(); print '+';  } ?>  
                      
                    <span class="edit"><?php edit_post_link('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', '  ', '  '); ?></span>  
                </div>  
            </div>  
            <!-- end: entry_title_box -->  
 
        <div class="entry">  
            <div class="page" id="post-<?php the_ID(); ?>">  
                <?php the_content('More &raquo;'); ?>  
                <div class="clear"></div>  
            </div>  
        <div class="rss">  
 
                  
<?php  
require_once (ABSPATH . WPINC . '/class-feed.php');  
 
//此处把知更鸟的简单str函数改为可以识别中文字函数的函数cut_strr,就不会乱码了。  
function cut_strr($str,$len) {  
 if (strlen($str) <= $len) return $str;  
 $n = 0;  
 $tempstr = '';  
 for ($i=0; $i<$len; $i++) {  
  if (ord(substr($str,$n,1)) > 224) {  
   $tempstr .= substr($str,$n,3);  
   $n += 3;  
   $i++; //把一个中文按两个英文的长度计算  
  } elseif (ord(substr($str,$n,1)) > 192) {  
   $tempstr .= substr($str,$n,2);  
   $n += 2;  
   $i++; //把一个中文按两个英文的长度计算  
  } else {  
   $tempstr .= substr($str,$n,1);  
   $n ++;  
  }  
}  
//此处增加strip_tags(),去掉字串中包含 HTML 及 PHP 的标记,返回字符串形式的值,避免输出值中有分段现象造成各种难看  
 $tempstr = strip_tags($tempstr);  
return $tempstr.'...';  
}  
 
$feed = new SimplePie();  
$feed->set_feed_url(array('http://feed.feedsky.com/synyan2')); //可以添加多个站点feed地址 
$feed->enable_order_by_date(false);  
$feed->set_cache_location($_SERVER['DOCUMENT_ROOT'] . '/cache');//缓存文件夹  
$feed->init();  
$feed->handle_content_type();  
?>  
<ul>  
<?php foreach ($feed->get_items(0,10) as$item)://10是文章篇数 ?>  
<li>  
<a href="<?php echo $item->get_permalink()?>" rel="bookmark" title="阅读> <?php echo $item->get_title()?>"><?php echo$item->get_title()?></a><!-- 文章标题 -->  
<br/><div style="margin-left: 20px; "><?php echo cut_strr($item->get_description(),360);?>  
<a href="<?php echo $item->get_permalink()?>" rel="bookmark" title="阅读">继续阅读</a><!-- 文章内容 --></div>  
<?php endforeach; ?>  
</li>  
</ul>  
 
            </div>  
        </div>  
        <!-- end: entry -->  
        <div class="clear"></div>  
 
        <?php comments_template(); ?>  
 
        <i class="lt"></i>  
        <i class="rt"></i>  
    </div>  
    <div class="entry_sb">  
        <i class="lb"></i>  
        <i class="rb"></i>  
    </div>  
    <?php endwhile; ?>  
    <?php endif; ?>  
</div>  
<!-- end: content -->  
<?php get_sidebar(); ?>  
<?php get_footer(); ?>

2012年4月8日

补记:

有个方法比这个好,请见《单篇文章嵌入不同的RSS或Atom feed地址》。

19

  1. ric83311

    上wordpress要翻墙的伤不起 :叹气

    Maxthon 3 · Windows XP
    1. S

      @ric83311自己建一个嘛……要不要我帮忙呀

      Google Chrome 16 · Windows 7
      1. ric83311

        @S谢过虎哥的好意先,哈哈,只怕我的懒惰辜负了虎哥一片好心啊,压力山大啊,还是努力翻墙比较轻松,心理上,啊哈哈哈哈 :谢谢

        Maxthon 3 · Windows 7
        1. S

          @ric83311哈哈,受虐光荣……

          Google Chrome 17 · Windows 7
          1. ric83311

            @S嘿嘿 :跑跑

            Maxthon 3 · Windows 7
  2. 勺子

    我是覚得这很影响页面的干净~~

    Google Chrome 18 · Mac OS X 10.5
    1. S

      @勺子不理解啥意思?

      Google Chrome 16 · Windows 7
  3. Aikvn

    第一次看到,还以为是竹山茅房……

    Google Chrome 18 · Windows 7
    1. S

      @Aikvn相由心生。

      Google Chrome 17 · Windows 7
  4. Healson

    稍微修改下,这不就是采集插件?用来窃取别人的劳动成果很有用。。。

    Firefox 11 · Windows XP
    1. S

      @Healson对的。

      Google Chrome 17 · Windows 7
  5. 13899.tk

    调用了,但是调来的文章标题有点长,有什么办法调用来的文章标题缩小吗?

    Google Chrome 19 · Windows XP
    1. S

      @13899.tk有的。标题截断函数在文章里提到了,使用 function cut_strr($str,$len) 即可。

      Google Chrome 17 · Windows 7
  6. 护法

    貌似很有用的功能,还没想出怎么用在我的博客上。:-)

    Unknown · Unknown
    1. S

      @护法你可以把我的内容显示在你的友情链接里……

      Unknown · Unknown
      1. 护法

        @S嗯,这周末研究一下。

        Unknown · Unknown

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注