/ 博客 / 1584浏览

单篇文章嵌入不同的RSS或Atom feed地址

在知更鸟的文章《单篇文章嵌入不同的RSS或Atom feed地址》中提到:

该功能主要用于转载文章时,为了尊重原文作者,也为了方便自己和其他阅读者及时掌握原文作者最新文章动态而折腾的一个小功能。

WordPress可以通过使用下面代码,在任意位置调用不同的RSS或Atom feed地址条目,代码来自:官方Codex

<?php     
include_once(ABSPATH . WPINC . '/rss.php');     
wp_rss($uri, $num);     
?>

在此基础上,利用Wordpress自定义栏目功能,改进后的代码:

<?php if ( get_post_meta($post->ID, 'feed', true) ) : ?>     
<p class="feeds" style="font-weight:bold;">本文作者最新文章</p>     
<?php include_once(ABSPATH.WPINC.'/rss.php');?>     
<?php $feed = get_post_meta($post->ID, 'feed', true); ?>     
<?php wp_rss($feed, 5); ?>     
<?php endif; ?>

将该代码加到主题文章页面模版single.php的适当位置,一般放到:

<?php the_content('Read more...'); ?>

的下面,也就是文章末尾。

使用方法:

编辑文章时,在自定义栏目名称中输入:feed,值:为调用的RSS或Atom feed链接地址。

默认显示5篇RSS地址最新文章,可以自行修改其中的数字,不添加自定义栏目“feed”则不显示该内容。


2012年4月8日

补记:今天也测试了一下这个方法,比《调用其它站点RSS文章显示在自己的WordPress网站页面上》方法好,可以自由调用和改换RSS地址,只要在自定义栏目里定义即可。但是输出格式不太好,要修改一下格式。

具体修改方法:

1、新增一个template(以知更鸟的主题为例。具体template需根据自己的主题适当修改):

<?php   
/*
Template Name: CSS Template  
*/  
?>   
  
<?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">   
            <div class="page" id="post-<?php the_ID(); ?>">   
                <?php the_content('More &raquo;'); ?>   
//23行到28行为主要部分,把代码插入到你所使用主题的恰当位置,一般是content下方。              
        <?php if ( get_post_meta($post->ID, 'feed', true) ) : ?>        
        <p class="feeds" style="font-weight:bold;">最新文章</p>        
        <?php include_once(ABSPATH.WPINC.'/rss.php');?>        
        <?php $feed = get_post_meta($post->ID, 'feed', true); ?>        
        <?php wp_rss($feed, 10); ?>        
        <?php endif; ?>      
  
        <div class="clear"></div>   
                <?php the_tags('标签: ', ', ', ' '); ?>   
                <span class="edit"><?php edit_post_link('<span class="edita">&nbsp&nbsp&nbsp&nbsp&nbsp</span>', '  ', '  '); ?></span>   
                   
                <?php if(function_exists('the_views')) { print ' 被围观 '; the_views();   } ?>   
            </div>   
        </div>   
  
        <!-- end: entry -->   
        <div class="clear"></div>   
        <i class="lt"></i>   
        <i class="rt"></i>   
    </div>   
    <div class="entry_sb">   
        <i class="lb"></i>   
        <i class="rb"></i>   
    </div>   
    <div class="ct"></div>   
    <?php comments_template(); ?>   
    <?php endwhile; ?><?php else : ?>   
    
    <p class="center">非常抱歉,无与之相匹配的信息。</p>   
    <?php include (TEMPLATEPATH . "/searchform.php"); ?>   
    <?php endif; ?>   
</div>   
<!-- end: content -->   
<?php get_sidebar(); ?>   
<?php get_footer(); ?>

2、在目录下wp-includes找到rss.php,把wp_rss()函数稍作修改,特别是第13行的 esc_attr(strip_tags($item['description'])),修改为esc_attr(cut_strr( $item['description'], 360)):

function wp_rss( $url, $num_items = -1 ) {   
    if ( $rss = fetch_rss( $url ) ) {   
        echo '<ul>';   
  
        if ( $num_items !== -1 ) {   
            $rss->items = array_slice( $rss->items, 0, $num_items );   
        }   
  
        foreach ( (array) $rss->items as $item ) {   
            printf(   
            '<li><a href="%1$s" title="" target=_blank>%4$s</a><br /><div style="margin-left: 20px; ">%2$s</div></li><br / >',   
                esc_url( $item['link'] ),   
                esc_attr( cut_strr( $item['description'], 360)),   
                esc_attr( strip_tags( $item['pubDate'])),   
                htmlentities($item['title'],ENT_NOQUOTES,"UTF-8")   
            );   
        }   
  
        echo '</ul>';   
    } else {   
    _e( 'An error has occurred, which probably means the feed is down. Try again later.' ); 
    }   
}   
endif;   
  
//此处增加识别中文字函数的函数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 ++;     
  }     
}     
$tempstr = strip_tags($tempstr);     
return $tempstr.'...';     
}

1

  1. ric83311

    又是技术贴 :跑跑

    Maxthon 3 · Windows XP

发表回复

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