Pagination on custom wp_query in WordPress takes to 404 error page(WordPress 中自定义 wp_query 的分页显示为 404 错误页面)
问题描述
我有一个 wp_query 循环,代码如下:
Im have a loop with wp_query with the following code:
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query("showposts=2&paged=$paged");
?>
<?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php the_title() ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'theme' ); ?></h2>
</article>
<?php endif; my_pagination(); wp_reset_query()?>
使用标准分页:
<?php
function my_pagination()
{
global $wp_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'prev_text' => __('<i class="fa fa-chevron-left"></i>'),
'next_text' => __('<i class="fa fa-chevron-right"></i>'),
'total' => $wp_query->max_num_pages,
));
}
?>
页面上的分页显示正确,但每当我单击分页链接时,它都会将我带到错误页面.
The pagination is showing correctly on the page, but whenever I click on the pagination link it takes me to the error page.
现在什么都试过了,不知道是什么原因.
Tried everything now and have no idea what can be the reason for it.
非常感谢艾米的帮助
推荐答案
我也遇到了困难 :) 当我意识到按页码计算的帖子是错误的时,搜索变得更容易了,这里有一个魔术:(添加到functions.php)
Had a hard time with it too :) Was easier to search when I realized it's wrong calculated post per page number, and here is a magic trick: (to be added to functions.php)
function my_post_count_queries( $query ) {
if (!is_admin() && $query->is_main_query()){
if(is_home()){
$query->set('posts_per_page', 1);
}
}
}
add_action( 'pre_get_posts', 'my_post_count_queries' );
这篇关于WordPress 中自定义 wp_query 的分页显示为 404 错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WordPress 中自定义 wp_query 的分页显示为 404 错误页面


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01