最近工作遇到调用blog里的内容,一般博客都是用wordpress建的,调用的时候会有两种情况,一种博客在同一个站点,一种不在同一个站点,比如二级域名。
同一个站点的话比较简单:
1 2 3 4 5 6 7 8 9 10 11
| <?php define('WP_USE_THEMES', false); require('blog/wp-blog-header.php');
query_posts('showposts=20'); ?> <?php while (have_posts()): the_post(); ?> <li><a href="<?php the_permalink(); ?>" target="_blank"> <?php echo mb_strimwidth(strip_tags(apply_filters('the_title', $post->post_title)), 0, 50," "); ?> </a></li> <?php endwhile; ?>
|
不在一个站点
不在同一站点的情况下,网上流传的方法是用了一款名为Ecall的插件,这个插件是JS调用的,不利于SEO。
一种方法是使用同一站点的方法,在博客根目录中新建blog_call.php文件,内容同站点中给出的代码,然后在需要调用的站点使用file读取
1 2 3 4 5 6
| <?php
f$str = ile_get_contents("http://www.blog.com/blog_call.php"); echo $str[0]; ?>
|
另一种方法是读取博客RSS的方式,下面这段PHP读取RSS的代码在网上流传已久,但是很多朋友不知道,其实它是可以用在WP外部调用上的··
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?php
$rssfeed = array("http://feeds2.feedburner.com/redlogpress", "http://rss.sina.com.cn/news/allnews/sports.xml", "http://ent.163.com/special/00031K7Q/rss_toutiao.xml");
header('Content-Type:text/html;charset= UTF-8'); for($i=0;$i<sizeof($rssfeed);$i++){
$buff = ""; $rss_str="";
$fp = fopen($rssfeed[$i],"r") or die("can not open $rssfeed");
while ( !feof($fp) ) { $buff .= fgets($fp,4096);
}
fclose($fp);
$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($parser,$buff,$values,$idx);
xml_parser_free($parser); foreach ($values as $val) { $tag = $val["tag"];
$type = $val["type"]; $value = $val["value"];
$tag = strtolower($tag); if ($tag == "item" && $type == "open"){
$is_item = 1; }else if ($tag == "item" && $type == "close") {
$rss_str .= "<a href='".$link."' target=_blank>".$title."</a><br />";
$is_item = 0; }
if($is_item==1){ if ($tag == "title") {$title = $value;}
if ($tag == "link") {$link = $value;} } }
echo $rss_str."<br />"; } ?>
|
这两种方法都是要求使用调用的站点支持PHP,如果不支持PHP而支持ASP的话可以用方法一,把读取blog_call.php的PHP代码用ASP重写一遍,但是如果是静态空间就只能装插件来实现了。
同域名下不同Wordpress间文章调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?php $loca="/blog"; require_once( dirname(__FILE__) . $loca.'/wp-load.php' ); wp(); ?> <?php query_posts('showposts=3'); while (have_posts()) : the_post(); ?><div> <?php the_excerpt();?> </div> <?php endwhile; ?>
<p align="center"> <?php global $paged, $wp_query; if($paged>1) echo '<a href='.str_replace($loca,'',get_previous_posts_page_link()).'>&laquo; Previous Entry</a> '; if($paged<$wp_query->max_num_pages) echo ' <a href='.str_replace($loca,'',get_next_posts_page_link()).'>Next Entry &raquo;</a>'; ?> </p>
|
PS:JS调用可以采用Feed to JS来实现。