Created
August 18, 2012 07:08
-
-
Save shobotch/3385008 to your computer and use it in GitHub Desktop.
WordPressにありがちなページナビをソース見ないでひとまずSmarty用に書いてみた(作りかけ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Smarty {get_page_info} function plugin | |
*/ | |
function smarty_function_get_page_info($param, &$smarty){ | |
//var_dump($param["page"]); | |
//現在のページ数からどのくらいの範囲のページを詳細に表示するか | |
//例:5の場合、手前は現在のページからdetailの半分の値で小数点以下切り捨てで、この場合2こ表示する。 | |
// 奥はdetailの数だけ表示するので、この場合4こ表示する。 | |
// よって、現在のページが10ページだとすると、 8 9 10 11 12 13 14 15 | |
$detail = 4; | |
$page = array(); | |
//パラメータ一覧 | |
//$param["page"]["nowpage"] | |
//$param["page"]["maxpage"] | |
//$param["page"]["flag"] | |
$start_point = $param["page"]["nowpage"] - floor($detail / 2); | |
$end_point = $param["page"]["nowpage"] + $detail; | |
if($start_point < 1){ | |
$start_point = 1; | |
} | |
$page[] = '<br><span class="pages">Page ' . $param["page"]["nowpage"] .' of ' . $param["page"]["maxpage"] . '</span><br><br>'; | |
if($param["page"]["nowpage"] > 2 and $start_point != 1){ | |
$page[] = '<a href="?page=1">« First</a>'; | |
} | |
if($param["page"]["nowpage"] >= 2){ | |
$page[] = '<a href="?page='.($param["page"]["nowpage"] - 1).'">«</a>'; | |
} | |
if($end_point > $param["page"]["maxpage"]){ | |
//詳細表示範囲が最後のページを超えている場合に開始位置をその分ずらす | |
$start_point -= $end_point - $param["page"]["maxpage"]; | |
if($start_point < 1){ | |
$start_point = 1; | |
} | |
} | |
if($start_point > 2){ | |
$page[] = '<span class="extend">...</span>'; | |
} | |
for($i = $start_point;$i <= $end_point and $i <= $param["page"]["maxpage"];$i++){ | |
if($param["page"]["nowpage"] == $i){ | |
$page[] = '<span class="current">' . $i .'</span>'; | |
}else{ | |
$page[] = '<a href="?page=' . $i . '">' . $i . '</a>'; | |
} | |
} | |
if(($param["page"]["maxpage"] - $end_point) > 1){ | |
$page[] = '<span class="extend">...</span>'; | |
} | |
if($param["page"]["maxpage"] > $param["page"]["nowpage"]){ | |
$page[] = '<a href="?page='.($param["page"]["nowpage"] + 1).'">»</a>'; | |
} | |
if(($param["page"]["maxpage"] - $end_point) > 0){ | |
$page[] = '<a href="?page=' . $param["page"]["maxpage"] . '">Last »</a>'; | |
} | |
/* | |
echo "start_point: ".$start_point.PHP_EOL; | |
echo "end_point: ".$end_point.PHP_EOL; | |
echo "<br>"; | |
*/ | |
$pages = implode(PHP_EOL . " ", $page); | |
//パラメータにassign | |
$smarty->assign($param["assign"], $pages); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{if isset($page) and is_array($page)} | |
<section class="pagenavi"> | |
{get_page_info page=$page assign=pages} | |
{$pages} | |
</section>{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ページナビ */ | |
.pagenavi { | |
font-weight: normal; | |
text-align: center; | |
/* border: 2px solid #F00;*/ | |
padding-top: 15px; | |
padding-right: 0px; | |
padding-bottom: 15px; | |
padding-left: 0px; | |
} | |
.pagenavi a, .pagenavi a:link { | |
padding: 1px 5px 1px 5px; | |
margin: 3px 4px; | |
text-decoration: none; | |
border: 1px solid #aaa; | |
color: #666; | |
background-color: #e7e8e3; | |
} | |
.pagenavi a:visited { | |
padding: 1px 5px 1px 5px; | |
margin: 3px 4px; | |
text-decoration: none; | |
border: 1px solid #aaa; | |
color: #666; | |
background-color: #e7e8e3; | |
} | |
.pagenavi a:hover { | |
border: 1px solid #0090db; | |
color: #fff; | |
background-color: #2f97cc; | |
} | |
.pagenavi a:active { | |
padding: 1px 5px 1px 5px; | |
margin: 3px 4px; | |
text-decoration: none; | |
border: 1px solid #aaa; | |
color: #666; | |
background-color: #e7e8e3; | |
} | |
.pagenavi span.pages { | |
padding: 1px 5px 1px 5px; | |
margin: 3px 4px; | |
color: #666; | |
border: 1px solid #aaa; | |
background-color: #e7e8e3; | |
} | |
.pagenavi span.current { | |
padding: 1px 5px 1px 5px; | |
margin: 3px 4px; | |
border: 1px solid #aaa; | |
color: #fff; | |
background-color: #2f97cc; | |
} | |
.pagenavi span.extend { | |
padding: 1px 5px 1px 5px; | |
margin: 3px 4px; | |
border: 1px solid #aaa; | |
color: #666; | |
background-color: #e7e8e3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment