使用WordPress内置的the_posts_pagination()函数纯代码添加分页按钮
在主题style.css中添加分页样式代码:
/* 自定义分页样式 */
.custom-pagination {
margin: 30px 0;
text-align: center;
}
.custom-pagination .nav-links {
display: inline-block;
}
.custom-pagination .page-numbers {
display: inline-block;
padding: 8px 15px;
margin: 0 5px;
border: 1px solid #ddd;
text-decoration: none;
color: #337ab7;
border-radius: 4px;
transition: all 0.3s ease;
}
.custom-pagination .page-numbers.current,
.custom-pagination .page-numbers:hover {
background-color: #337ab7;
color: white;
border-color: #337ab7;
}
.custom-pagination .page-numbers.dots {
border: none;
padding: 8px 5px;
}
.custom-pagination .page-numbers.prev,
.custom-pagination .page-numbers.next {
font-weight: bold;
}
在需要引用的分页的地方添加如下自定义分页代码:
<!-- 使用WordPress内置分页功能替代kriesi_pagination --> <div class="custom-pagination"> <?php the_posts_pagination( array( 'mid_size' => 2, 'prev_text' => __( '« Previous', 'twentytwelve' ), 'next_text' => __( 'Next »', 'twentytwelve' ), ) ); ?> </div>
category.php中引用在endwhile;后添加?>来关闭PHP模式,然后添加HTML的分页容器代码,在HTML中再使用<?php打开PHP标签来调用分页函数。
美化版CSS
/* 自定义纯代码分页样式 - 超紧凑版 */
.custom-pagination {
margin: 20px 0;
text-align: center;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.custom-pagination .nav-links {
display: inline-block;
}
.custom-pagination .page-numbers {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 28px;
height: 28px;
padding: 0 8px;
margin: 0 2px;
border: 1px solid #4b6cb7;
text-decoration: none;
color: #4b6cb7;
border-radius: 4px;
transition: all 0.2s ease;
background: white;
font-weight: 500;
font-size: 12px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}
.custom-pagination .page-numbers:hover {
background: #4b6cb7;
color: white;
transform: translateY(-1px);
box-shadow: 0 2px 5px rgba(75, 108, 183, 0.25);
}
.custom-pagination .page-numbers.current {
background: #4b6cb7;
color: white;
border-color: #4b6cb7;
box-shadow: 0 1px 4px rgba(75, 108, 183, 0.3);
}
.custom-pagination .page-numbers.dots {
border: none;
padding: 0 4px;
background: transparent;
box-shadow: none;
color: #6c757d;
min-width: auto;
}
.custom-pagination .page-numbers.prev,
.custom-pagination .page-numbers.next {
font-weight: 600;
min-width: 60px;
background: #2c3e50;
color: white;
border-color: #2c3e50;
font-size: 11px;
}
.custom-pagination .page-numbers.prev:hover,
.custom-pagination .page-numbers.next:hover {
background: #4b6cb7;
border-color: #4b6cb7;
}
/* 响应式设计 */
@media (max-width: 768px) {
.custom-pagination .nav-links {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 3px;
}
.custom-pagination .page-numbers {
margin: 1px;
min-width: 26px;
height: 26px;
font-size: 11px;
}
.custom-pagination .page-numbers.prev,
.custom-pagination .page-numbers.next {
min-width: 50px;
font-size: 10px;
}
}