分頁按鈕的學習,在wordpress開發中制作頁面的分頁按鈕
分頁功能是在wordpress主題開發時常用的功能,用于引導當前頁面進行翻頁,在查閱大量文章時會用到。
使用posts_nav_link 函數,
- 函數文檔:官方
代碼部分:
<!--底部分頁按鈕-->
<div class="navigation">
<div class="alignleft"><?php previous_posts_link( '上一頁' ); ?></div>
<div class="alignright"><?php next_posts_link( '下一頁', '' ); ?></div>
</div>
CSS:
/*底部按鈕樣式*/
.navigation{
overflow: hidden;
}
.alignleft{
float: left;
margin-left: 1px;
}
.alignright{
float: right;
}
.navigation div a{
border: 1px solid black;
border-radius: 28px;
padding: 0.48em 2em;
line-height: 2.6em;
-webkit-transition: 0.3s ease all;
-moz-transition: 0.3s ease all;
-ms-transition: 0.3s ease all;
-o-transition: 0.3s ease all;
transition: 0.3s ease all;
color: black;
}
.navigation div a:hover{
background-color: black;
color: white;
}
集成函數
將以下代碼添加至主題根目錄下functions.php
文件的<?php
下面:
function fenye(){
$args = array(
'prev_next' => 0,
'format' => '?paged=%#%',
'before_page_number' => '',
'mid_size' => 2,
'current' => max( 1, get_query_var('paged') ),
'prev_next' => True,
'prev_text' => __('上一頁'),
'next_text' => __('下一頁'),
);
$page_arr=paginate_links($args);
if ($page_arr) {
echo $page_arr;
}else{
}
}
調用:
<?php fenye(); ?>