Npcink Blog
切换菜单

怎么实现分页导航功能? - wordpress 开发

分页按钮的学习,在 wordpress 开发中制作页面的分页按钮

本页目录2 节

分页功能是在 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(); ?>