首页
PHP
WordPress paginate_links - 如何使用它?
WordPress paginate_links - 如何使用它?
[英] WordPress paginate_links - how to use it?
本文介绍了WordPress paginate_links - 如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为带有子页面的页面添加数字分页.这是我想要创建的分页(从引导程序):
<li><a href="#" aria-label="Previous"><span aria-hidden="true">«</span></a><li><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li><li><a href="#" aria-label="下一个"><span aria-hidden="true">»</span></a> </nav>
这是我的代码:
<div class="row grid events-block"><?php$parent = $post->ID;//query_posts('posts_per_page=15&post_type=page&post_parent='.$parent);query_posts(array('posts_per_page'=>'1', 'post_type' =>'page', 'post_parent' => $parent, 'paged' => get_query_var('paged')));while (have_posts()) : the_post();?><!-- 项目--><div class="grid-item col-md-6 col-sm-6 col-xs-12 event-item"><div class="date-box"><?php echo $event_dates;?>
<div class="event-item-text-box"><div class="event-item-text-inner-box"><h3 class="heading-item"><a href="#" target="_blank"><?php the_title();?></a></h3><p><?php the_excerpt();?></p>
<!-- 项目--><?php endwhile;?><?php//将帖子重置为原来的 after 循环.否则当前页面将成为 while 循环的最后一项.//https://codex.wordpress.org/Function_Reference/wp_reset_querywp_reset_query();?>
<!-- 块--><?php $args = 数组('基础' =>'%_%','格式' =>'?paged=%#%','总计' =>1、'当前' =>0,'show_all' =>错误的,'end_size' =>1、'mid_size' =>2、'prev_next' =>真的,'prev_text' =>__('"以前的'),'next_text' =>__('下一个 "'),'类型' =>'清楚的','add_args' =>错误的,'add_fragment' =>'','before_page_number' =>'','after_page_number' =>'');?><?php echo paginate_links( $args );?>
但我无法让它工作.有什么想法我错过了吗?
解决方案
分页喜欢:上一页 1 2 3 下一页
'YOUR_POST_TYPE',//你的帖子类型名称'posts_per_page' =>3,//每页发帖'分页' =>$分页,));if($data->have_posts()) :while($data->have_posts()) : $data->the_post();//你的代码终了;$total_pages = $data->max_num_pages;如果 ($total_pages > 1){$current_page = max(1, get_query_var('paged'));回声分页链接(数组('基础' =>get_pagenum_link(1) .'%_%','格式' =>'/页/%#%','当前' =>$current_page,'总计' =>$total_pages,'prev_text' =>__('«上一个'),'next_text' =>__('下一个 "'),));}?><?php 其他 :?><h3><?php _e('404 Error: Not Found', '');?></h3><?php endif;?><?php wp_reset_postdata();?>
你能试试上面的代码吗?我觉得对你有帮助.
I want to add a numberic pagination to a page with its child pages. This is the pagination I would want to create (from bootstrap):
<nav>
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
And this is my code:
<!-- block -->
<div class="row grid events-block">
<?php
$parent = $post->ID;
// query_posts('posts_per_page=15&post_type=page&post_parent='.$parent);
query_posts(array('posts_per_page'=>'1', 'post_type' => 'page', 'post_parent' => $parent, 'paged' => get_query_var('paged')));
while (have_posts()) : the_post();
?>
<!-- item -->
<div class="grid-item col-md-6 col-sm-6 col-xs-12 event-item">
<div class="date-box">
<?php echo $event_dates; ?>
</div>
<div class="event-item-text-box">
<div class="event-item-text-inner-box">
<h3 class="heading-item"><a href="#" target="_blank"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div>
<!-- item -->
<?php endwhile; ?>
<?php
// Reset the post to the original after loop. otherwise the current page becomes the last item from the while loop.
// https://codex.wordpress.org/Function_Reference/wp_reset_query
wp_reset_query();
?>
</div>
<!-- block -->
<?php $args = array(
'base' => '%_%',
'format' => '?paged=%#%',
'total' => 1,
'current' => 0,
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
); ?>
<?php echo paginate_links( $args ); ?>
But I can't get it working. Any ideas what I have missed?
解决方案
Pagination Like : Prev 1 2 3 Next
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>'YOUR_POST_TYPE', // your post type name
'posts_per_page' => 3, // post per page
'paged' => $paged,
));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();
// Your code
endwhile;
$total_pages = $data->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
Could you please try above code? I think it's helpful for you.
这篇关于WordPress paginate_links - 如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!