wp_archive.php上的查询分页

wp_archive.php上的查询分页,php,wordpress,pagination,Php,Wordpress,Pagination,我在Wordpress的几个页面上使用了下面的wp_查询,您可以看到,我正在尝试确保查询分页。在自定义页面模板page-articles.php上,一切都很成功,但是我无法在archive.php模板上获得相同的结果 分页链接成功呈现(例如mydomain.com/category/my cat/page/2),但是单击该链接不起作用,它只是抛出一个404错误?这些链接怎么可能不去任何地方 我假设在archive.php模板上使用自定义wp_查询存在一些问题 谢谢 查询 $args = arra

我在Wordpress的几个页面上使用了下面的wp_查询,您可以看到,我正在尝试确保查询分页。在自定义页面模板page-articles.php上,一切都很成功,但是我无法在archive.php模板上获得相同的结果

分页链接成功呈现(例如mydomain.com/category/my cat/page/2),但是单击该链接不起作用,它只是抛出一个404错误?这些链接怎么可能不去任何地方

我假设在archive.php模板上使用自定义wp_查询存在一些问题

谢谢

查询
$args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 1,
    'paged'             => $paged,
    'orderby'           => 'date',
    'order'             => 'DESC'
);
$articles = new WP_Query($args );
?>
循环

$args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 1,
    'paged'             => $paged,
    'orderby'           => 'date',
    'order'             => 'DESC'
);
$articles = new WP_Query($args );
?>
<?php if ( $articles->have_posts() ) : ?>
   <?php while ( $articles->have_posts() ) : $articles->the_post(); ?>
       Posts here!
   <?php endwhile; ?>
   <?php wp_reset_postdata(); ?>
<?php endif; ?>

张贴在这里!
分页

$args = array(
    'post_type'         => 'post',
    'posts_per_page'    => 1,
    'paged'             => $paged,
    'orderby'           => 'date',
    'order'             => 'DESC'
);
$articles = new WP_Query($args );
?>
<nav>
    <div class="prev"><?php echo get_previous_posts_link( 'Previous', $articles->max_num_pages );   ?></div>
    <div class="next"><?php echo get_next_posts_link( 'Next', $articles->max_num_pages ); ?></div>
</nav>

是的,是的

这是因为在使用WP_查询时,archive.php的主查询保持不变。尝试在archive.php中使用query_posts()

query_posts($args);
然后是默认循环(而不是$articles)


张贴在这里!

经过一番探索,在他的帮助下,下面的解决方案解决了这个问题。只要把它放在functions.php文件中就可以了。以下实现适用于自定义帖子类型和类别的归档

/**
 * Wordpress has a known bug with the posts_per_page value and overriding it using
 * query_posts. The result is that although the number of allowed posts_per_page
 * is abided by on the first page, subsequent pages give a 404 error and act as if
 * there are no more custom post type posts to show and thus gives a 404 error.
 *
 * This fix is a nicer alternative to setting the blog pages show at most value in the
 * WP Admin reading options screen to a low value like 1.
 *
 */
function custom_posts_per_page( $query ) {

    if ( $query->is_archive('cpt_name') || $query->is_category() ) {
        set_query_var('posts_per_page', 1);
    }
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );

你好@elvin85,谢谢你的回复。I jsut实现了您的解决方案,但是它只输出相同的分页链接(例如–问题实际上似乎是这些页面实际上不存在(尽管有很多帖子可以创建它们).Wordpress显然认为它们的存在是为了输出链接,但它们没有?我已重置永久链接,但没有帮助?如果分页链接存在,但出现404错误,这意味着您需要使用pre_get_posts()过滤器,并在那里使用$query->set()设置参数method.OK好极了-您能帮助调整原始代码以适应这种方法吗?谢谢!对于您需要添加到函数中的部分,有现成的答案。php.f.e.谢谢,我不敢相信对于如此简单的任务,这需要如此复杂?例如,我根本没有使用自定义分页函数?