分页按钮的学习,在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(); ?>