他のブログのコンテンツを表示する

別のワードプレスの情報を表示する方法として、WidgetでRSSを表示したり、json プラグインを使ったりする方法が知られています。

ワードプレスは、jsonを発行させるための仕組みも持っています。 http://d.hatena.ne.jp/tenman/20110307/p1(例)


もっと、簡単に、自前でそのような作業を、柔軟に行える例をメモに残します。

ページテンプレートで、jsonを発行して、その情報をjavascriptで受ける。
jsonは、get_postsで取得するので、自分の必要な情報を柔軟に受け取れる

json.phpの作成

<?php
/*
Template Name: json
 */
?>
<?php
 $json= get_posts( array( 'numberposts'=>10 ) );
 $json= json_encode( $json );
 $json= str_replace("\n","", $json );
 
	ob_start(); 
		var_export( $json );
		$json= ob_get_contents();
	ob_end_clean();
	
	$json= trim($json,'\'');
	
	header("Content-Type: text/javascript; charset=utf-8"); 
	echo stripslashes( $json );
?>

表示する側、jQuery使うので、どんなとこでもヘイチャラ

<html>
<head>
 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
 <script src="http://nje.github.com/jquery-tmpl/jquery.tmpl.js"></script>
	<script id="titleList" type="text/x-jquery-tmpl"> 
	 <li>${post_date}&amp;nbsp;&amp;nbsp;<strong><a href="${guid}">${post_title}</a></strong></li>
	</script>
</head>
<body>
<ul id="theList" style="list-style:none"></ul>
	<script>
<?php	$url = "http://example.com/local-wp/?page_id=1232";
	$m = file_get_contents($url);
		echo 'var lists_data='. $m.';'; ?>
		$( "#titleList" ).tmpl( lists_data ).appendTo( "#theList" );
	</script>
</body>
</html>

http://example.com/local-wp/?page_id=1232は、ページテンプレートを使って作ったページのURL

ワイルドだろぉ

[emulsion_relate_posts]