Wordpress CPT 分页错误第 1 页和第 2 页没问题,但后面的就不行了

Wordpress CPT pagination error 1st and 2nd page are fine but not the ones after

此时我已经尝试了好几天了,但我无法让它工作.. 我想在 wordpress 中创建一个 posts 页面,其中包含我所有的 posts 和我的 CPT。我通过在 index.php 中使用自定义查询创建一个 post 循环来执行此操作,该循环打印正常。然后我尝试通过 paginate_links() 函数使其分页,但是单击它时出现 404 错误。这是一个常见的问题。关键是它不会在第一页或第二页上执行此操作,它只会在第三页及以上页上执行。这段代码的一个更奇怪的事情是它在我的工作计算机上和在我的家用计算机上的反应不同(它们是不同的本地服务器,每种 post 类型的 posts 数量不同。我猜这就是它们工作方式不同的原因)无论如何,在我的工作计算机上,第二页也给我一个错误。

我已经将阅读设置更改为每页最多 1 post。我不知道我还能做错什么。

这是我的代码:

<?php 
$paged = ( get_query_var( 'paged') ) ? get_query_var( 'paged') : 1;

// custom post loop
$arg_custom_post_loop = array(
    'post_type' => array('post', 'ljm_utstallningar', 'ljm_event', 'ljm_arkiv', 'ljm_bildarkiv', 'ljm_film', 'ljm_pag'), 
    'post_status' => 'publish',
    'paged' => $paged,
    'posts_per_page' => 5,
);

$ljm_custom_post_loop = new WP_Query($arg_custom_post_loop);

$temp_query = $wp_query;
$wp_query = null;
$wp_query = $ljm_custom_post_loop;

if ($ljm_custom_post_loop->have_posts()) {
    while ($ljm_custom_post_loop->have_posts()) {
        $ljm_custom_post_loop->the_post();
        get_template_part('template/default-content-thumbnail', get_post_type());
    }
    echo paginate_links();

}
$wp_query = null;
$wp_query = $temp_query;
?>

感谢您的帮助

好的,我可以使用这段代码:

<?php 
// fixing pagination for custom loop

if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
    // code...
}else{
    $url= $_SERVER['REQUEST_URI'];
    $url = parse_url($url);
}

$current_page = explode('/', $url['query']);
$current_page = end($current_page);
 if (!is_numeric($current_page)) {
    $current_page = 1;
 }

// custom post loop
$arg_custom_post_loop = array(
    'post_type' => array('post', 'ljm_utstallningar', 'ljm_event', 'ljm_arkiv', 'ljm_bildarkiv', 'ljm_film', 'ljm_pag'), 
    'post_status' => 'publish',
    'paged' => $current_page,
    'posts_per_page' => 5,
);

$ljm_custom_post_loop = new WP_Query($arg_custom_post_loop);

$temp_query = $wp_query;
$wp_query = null;
$wp_query = $ljm_custom_post_loop;

if ($ljm_custom_post_loop->have_posts()) {
    while ($ljm_custom_post_loop->have_posts()) {
        $ljm_custom_post_loop->the_post();
        get_template_part('template/default-content-thumbnail', get_post_type());
    }

    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $ljm_custom_post_loop->max_num_pages,
    ));
    wp_reset_postdata();
}
$wp_query = null;
$wp_query = $temp_query;
?>

它不漂亮但很管用,我把 paginate_links() 默认操作从 window 中删除了,只是用它来打印 url 中的页码。然后我从 url 中检索数字并将其推送到 pagedcurrent 参数中。

感谢@CBroe 帮助我理解了为什么我的第一个代码不起作用:)