WordPress 自定义分类分页不起作用
我正在使用 WP PageNavi 插件进行分页。当 WP PageNavi 关闭时,无法对taxonomy-portflio-category.php 页面进行分页的特殊问题也是一个问题。
我在主页和页面模板页面上让分页工作花了很长时间,但我确实让它们工作了。这是他们的代码:
page-home.php(用作称为“主页”的静态首页上的页面模板)
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
分页有效!
page-portfolio.php(用作名为“工作”的页面上的页面模板)
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
分页有效!
taxonomy-portfolio-category.php(用作显示作品集部分的方式,例如打印、摄影等)
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
global $wp_query;
query_posts( array_merge( $wp_query->query, array( 'posts_per_page' => 2 ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi();
}
第 1 页 (/portfolio/interactive/) 看起来很棒!它肯定只发布 2 个项目,并且它会计算正确的分页页数。但是,当您单击第 2、3 或 4 页 (/portfolio/interactive/page/2/) 时,站点默认为 index.php 并显示“找不到页面”。分页失败!
希望我能尽快解决这个问题。我见过很多人在自定义分类页面上遇到同样的分页问题,但没有可靠的解决方案。请帮忙!
I’m using the WP PageNavi plugin for pagination. This particular problem in not getting the taxonomy-portflio-category.php page to paginate is also a problem when WP PageNavi is turned off.
I’ve had a heck of a time getting pagination to work on the homepage and on a page template page, but I did get them to work. Here’s their code:
page-home.php (used as a Page template on a static front page called “Home”)
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
page-portfolio.php (used as a Page template on a Page called “Work”)
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
taxonomy-portfolio-category.php (used as a way to display portfolio sections e.g. print, photography, etc.)
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
global $wp_query;
query_posts( array_merge( $wp_query->query, array( 'posts_per_page' => 2 ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi();
}
Page 1 (/portfolio/interactive/) looks great! It’s definitely only posting 2 items and it calculates the correct number of pagination pages. But when you click on page 2 or 3 or 4 (/portfolio/interactive/page/2/) the site defaults to index.php and shows “Page not found”. Pagination fails!
Hopefully I can resolve this soon. I’ve seen A LOT of people with this same problem of pagination on custom taxonomy pages, but no solid solutions. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在设置 -> 中将每页帖子数设置为 24 WP 管理中的阅读页面。希望这对某人有帮助。
You Need to set posts per page to 24 on the Settings -> Reading page in WP admin. Hope this helps someone.
我尝试过使用WP-Pagenavi,但它从来没有工作过,所以我使用了Wordpress本身的分页,我使用了Twentyfourteen的twentyfourteen_paging_nav()函数,因为它有一个分类页面,这里是代码:
I have tried using WP-Pagenavi but it never worked so i used the pagination from Wordpress it self, i used the twentyfourteen_paging_nav() function form Twentyfourteen becuse it has a taxonomy page, here is the code:
我遇到了类似的问题,我花了几个小时谷歌搜索!我终于找到了解决方案。
将以下代码添加到主题文件夹中的functions.php:
解决方案的 URL
I ran into similar issue, it took me hours of googling! I found the solution at last.
Add the following code to functions.php in your theme folder:
URL for the solution
可能您需要启用搜索才能启用分页
在声明自定义分类法时,您应该禁用搜索排除。
这解决了我的问题。
May be you need to enable search to enable pagination
While declaring custom taxonomy you should disable search excluding.
This fixed my problem.
我想分享以下解决方案(将此代码添加到主题中的
functions.php
中):来源
I would like to share following solution (add this code to
functions.php
in your theme):source