Wordpress分页自定义帖子类型 [英] Wordpress paginate custom post type
问题描述
我有一个名为新闻"的页面(使用页面模板page-newslist.php
),该页面应该显示来自也称为新闻"的自定义帖子类型的帖子.我意识到使用相同的名称会导致问题,因此在注册自定义帖子类型时,我进行了重写以区别于页面:
I have a page called News (using page template page-newslist.php
), which is supposed to display posts from the custom post type also named News. I realize that having both with the same name causes issues, so when registering the custom post type, I have a rewrite to differentiate it from the page:
'rewrite' => array('slug' => 'news-article', 'with_front' => true),
我可以使查询正常工作并正确显示帖子,但是在所有所有文章之后 >
I can get the query working and displaying the posts properly, but after all of the articles and posts I've read, I cannot get the pagination to work. Nothing ever shows up for the pagination.
在使用页面模板查询未成功后,我尝试了archive-news.php
方法,该方法将自动显示来自自定义帖子类型的帖子.分页在那儿工作.使用此方法的缺点是没有一个物理"页面将其绑定到该页面(该页面也具有自定义字段,可以很好地添加(而不是硬编码)到菜单中,等等)
After no success with using the page template query, I tried the archive-news.php
method, where it would automatically display the posts from the custom post type. The pagination does work there. The downside of using this method is that there isn't a 'physical' page to tie it to (which would also have custom fields, ability to be nicely added (not hard-coded) into menus, etc.)
以下是注册自定义帖子类型的精简代码:
Here is the stripped-down code registering the custom post type:
register_post_type('news', array(
'label' => 'News',
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'news-article', 'with_front' => true),
'query_var' => true,
'has_archive' => true,
));
然后是页面模板的代码:
And then the code for the page template:
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args = array(
'post_type' => 'news',
'post_status' => 'publish',
'posts_per_page' => 1,
'paged' => $paged
);
$my_query = null;
$my_query = new WP_Query($args);
if($my_query->have_posts()):
while ($my_query->have_posts()) : $my_query->the_post();
...
endwhile;
endif;
wp_reset_query();
// Attempt method 1
posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »'));
// Attempt method 2
previous_posts_link('« Newer');
next_posts_link('Older »');
有什么想法吗?
推荐答案
看看下一页链接页面,这里的示例会有所帮助. codex.wordpress.org/Template_Tags/next_posts_link
Have a look at next page link page, the example here will help. codex.wordpress.org/Template_Tags/next_posts_link
<?php next_posts_link('Older Entries »', 0); ?>
Wordpress Codex示例.
Wordpress codex example.
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after our query
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
这篇关于Wordpress分页自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!