wordpress wp_查询分页和自定义post_类型问题

wordpress wp_查询分页和自定义post_类型问题,wordpress,pagination,Wordpress,Pagination,我正在尝试对两种帖子类型的结果进行分页,这两种类型是一种自定义帖子类型question和一种普通的post 首先,当我使用'posts\u per_page'=>-1显示所有结果而不分页时该函数工作正常 但是当我尝试进行分页时会出现问题,因为您可以看到分页功能正常工作,但分页的最后几页除外(我猜其中包含问题post_类型,甚至最近的post类型的问题也会正常出现) 这是我的完整代码 <?php $my_query = new WP_Query( arra

我正在尝试对两种帖子类型的结果进行分页,这两种类型是一种自定义帖子类型
question
和一种普通的
post
首先,当我使用
'posts\u per_page'=>-1显示所有结果
而不分页时
该函数工作正常

但是当我尝试进行分页时会出现问题,因为您可以看到分页功能正常工作,但分页的最后几页除外(我猜其中包含问题post_类型,甚至最近的post类型的问题也会正常出现)

这是我的完整代码

 <?php

    $my_query = new WP_Query(
        array(
            'post_type' => array('question', 'post'),
            'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
            'author' => $post->post_author,
            'paged' => get_query_var('paged') ? get_query_var('paged') : 1
        )
    );


?>
<?php 
    while ( $my_query->have_posts() ) : $my_query->the_post();
?>

<span class="title">
    <?php echo get_post_type(); ?>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?>
    </a>
</span>
<br />

<?php endwhile;  ?>  
<?php 
    $big = 999999999; // need an unlikely integer
    echo paginate_links(
        array(
            'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $my_query->max_num_pages
        )
    );
?>



如何使分页正常工作并导航到所有分页链接?

替换您的代码:

 <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ($paged == "1") {
    $args = array(
            'post_type' => array('question', 'post'),
            'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
            'author' => $post->post_author,
            'offset' => 0
    );
} else {
    $offset = $paged * 5;
    $offset = $offset - 5;
    $args = array(
            'post_type' => array('question', 'post'),
            'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
            'author' => $post->post_author,
            'offset' => $offset
    );
}

$loop = new WP_Query($args);
?>
<?php 
    if ($loop->have_posts()) :while ($loop->have_posts()) : $loop->the_post();
?>

<span class="title">
    <?php echo get_post_type(); ?>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?>
    </a>
</span>
<br />

<?php endwhile;  ?>  
<div class="pagination-grp">
    <?php
    $big = 999999999; // need an unlikely integer
    //$i=1;

    echo paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'prev_text' => __('<'),
        'next_text' => __('>'),
        'total' => $loop->max_num_pages

    ));
    wp_reset_postdata();
    endif;
    ?>
</div>



将您的代码替换为:

 <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ($paged == "1") {
    $args = array(
            'post_type' => array('question', 'post'),
            'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
            'author' => $post->post_author,
            'offset' => 0
    );
} else {
    $offset = $paged * 5;
    $offset = $offset - 5;
    $args = array(
            'post_type' => array('question', 'post'),
            'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
            'author' => $post->post_author,
            'offset' => $offset
    );
}

$loop = new WP_Query($args);
?>
<?php 
    if ($loop->have_posts()) :while ($loop->have_posts()) : $loop->the_post();
?>

<span class="title">
    <?php echo get_post_type(); ?>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?>
    </a>
</span>
<br />

<?php endwhile;  ?>  
<div class="pagination-grp">
    <?php
    $big = 999999999; // need an unlikely integer
    //$i=1;

    echo paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'prev_text' => __('<'),
        'next_text' => __('>'),
        'total' => $loop->max_num_pages

    ));
    wp_reset_postdata();
    endif;
    ?>
</div>


这将显示分页,并像一个魅力为您工作


这将显示分页功能,对您来说非常有吸引力。

我想我已经明白了 解决方案是将WP_查询参数编辑为一个自定义编号,该编号与每个页面的站点帖子不同

'posts_per_page' => 11, // 13, or 20 ...
而不是

'posts_per_page' => get_option('to_count_portfolio'),
为什么??我不知道,也许这是某种冲突
有人可以尝试一下,看看他/她是否也能像我一样使用这个解决方案。我想我已经找到了答案 解决方案是将WP_查询参数编辑为一个自定义编号,该编号与每个页面的站点帖子不同

'posts_per_page' => 11, // 13, or 20 ...
而不是

'posts_per_page' => get_option('to_count_portfolio'),
为什么??我不知道,也许这是某种冲突
有人可以尝试一下,看看这个解决方案对他/她是否和我一样有效

Thank you@Arpan我用你的代码更改了我的代码,但我仍然用你的代码更改了404 last谢谢你@Arpan我用你的代码更改了我的代码,但我仍然用你的代码更改了404 last谢谢你@prashant,我更喜欢自己做,不需要额外的插件,或者至少创建我自己的插件hank you@prashant,我更喜欢自己做,而不需要额外的插件,或者至少创建我自己的插件