投稿のバックナンバーを表示する
<?php
//echo "このブログには、{$published_posts}個の投稿があります。";
function my_backnumber($min=1,$max=5,$separater='|'){
global $posts;
global $post;
if($max== 0){
$count_posts = wp_count_posts();
$max = $count_posts->publish;
}
query_posts("order=ASC&showposts=-1");
if(have_posts()){
$i = 0;
$result= "";
$memo = "";
/**
* ハイパーリンクの作成
*
*
*
*
*/
while (have_posts()) : the_post();
if($i>=$min and $i<=$max){
if(get_permalink( $post->ID )== "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']){
$class= "current";
$memo= $i;
}else{
$class= "back-number-link";
}
$result .= "<a href=\"". $post->guid."\" class=\"$class\">$i</a>";
}
$i++;
endwhile;
}
/**
* リンクセパレータの追加と、prev next リンクの追加
*
*
*
*
*/
if($memo > $min or $memo < $max - 1){
$b= $memo - 1;
$a= $memo + 1;
if(preg_match(" |(<a[^>]+>)($b)(</a>) |",$result,$regs)){
$before= $regs[1]."prev".$regs[3]." « ";
}else{
$before= "";
}
if(preg_match(" |(<a[^>]+>)($a)(</a>) |",$result,$regs) and !empty($memo)){
$after= " » ".$regs[1]."next".$regs[3];
}else{
$after= "";
}
$result= str_replace('a><a','a>'.$separater.'<a',$result);
echo "<div class=\"back-number\">".$before.$result.$after."</div>";
}
wp_reset_query();
}
add_filter('wp_head','back_number_style');
function back_number_style($content){
echo $content.'<style type="text/css">a.current{color:red;}</style>';
}
?>