在Wordpress中为自定义帖子类型创建分页 [英] Create pagination for custom post types in Wordpress
问题描述
我有一个活动页面,我想添加分页。但是,由于它是自定义帖子类型,所以我发现它相当困难。我已经设法使分页适用于我的新闻页面,但无法获得与事件页面相同的结果。这是我用于事件页面的代码
I have an events page which I would like to add pagination to. However, with it being a custom post type I am finding it rather difficult. I have managed to get pagination working for my news page, but I cannot get the same result for the events page. Here is my code for the events page
<?php
get_header();
get_sidebar('left');
?>
<article class="content-main events" role="main" id="post-<?php the_ID() ?>">
<?php include 'breadcrumbs.php'; ?>
<?php query_posts(array('posts_per_page'=>'2')); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="news-post">
<h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
<?php the_excerpt() ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<!--Pagination-->
<?php echo paginate_links( $args ) ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</article><!-- //.content-main -->
<?php
get_footer();
如果我尝试更改此
<?php query_posts(array('posts_per_page'=>'2')); ?>
对此
<?php query_posts(array('category_name'=>'events','posts_per_page'=>'2')); ?>
这也不起作用。但是,如果我完全删除该行,它将显示新闻发布类型。我很困惑!
This doesnt work either. However, if I remove the line altogether, it shows up the news post types. I am stumped!
推荐答案
自定义帖子类型的分页应与普通帖子相同。
Pagination for custom post types should work the same as normal posts.
如果您查看默认主题 TwentyEleven ,您将看到如何
If you take a look at the default theme TwentyEleven you can see how they do it.
基本上,他们使用 next_posts_link()和 previous_posts_link()函数。
Basically they use next_posts_link() and previous_posts_link() functions.
您可以在functions.php中查看它> twentytwelve_content_nav();
You can look at that in functions.php > twentytwelve_content_nav();
干杯,
这篇关于在Wordpress中为自定义帖子类型创建分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!