wordpress wp_Query与自定义post_type问题的分页

时间:2017-07-12 09:41:32

标签: wordpress pagination

我正在尝试对两种帖子类型的结果进行分页,这两种帖子类型是一种自定义帖子类型question和普通post 首先,当我使用without pagination显示所有结果'posts_per_page' => -1;时,该功能正常运行

enter image description here

但问题发生在我尝试制作分页时,你可以看到分页功能正常工作,除了分页的最后一页(我想其中包含问题post_type甚至最近的帖子类型的问题正常出现)

enter image description here

enter image description here

enter image description here

在这里,您是我的完整代码

 <?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
        )
    );
?>

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

3 个答案:

答案 0 :(得分:2)

替换你的代码:

 <?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>

答案 1 :(得分:1)

install this free plugin https://wordpress.org/plugins/wp-pagenavi/
then refer below code

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

$args = array (
    'post_type'              => 'post',
    'cat'                    => '53',
    'paged' => $paged,  
    'posts_per_page'         => '10',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        get_template_part('parts/bl`enter code here`ogloop');

    }


    if(function_exists('wp_pagenavi'))
        {
            wp_pagenavi( array( 'query' => $query ) );
        }

    wp_reset_postdata();

} else {
    echo 'no posts here';
}

这将显示分页和工作就像你的魅力。

答案 2 :(得分:0)

我想我明白了 解决方案是将WP_Query args编辑为自定义数字,该数字与网站posts_per_page不同

function my_custom_styles() {        
    wp_enqueue_style( 'my-custom-style', get_template_directory_uri() . '/custom-style.css');
}
add_action( 'wp_enqueue_scripts', 'my_custom_styles' );

而不是

'posts_per_page' => 11, // 13, or 20 ...

为什么?我不知道,也许是某种冲突 有人可以尝试一下,看看这个解决方案是否与我/她一样适用