自定义wordpress循环分页不起作用

时间:2015-10-27 10:42:07

标签: php wordpress pagination

我有一个网站,我在一个页面上有一些来自不同类别的循环。帖子显示就像我想要的那样。在页面底部,我想显示网站上的所有帖子。我也让这个工作正常。但是当我点击"较新的帖子时,这些帖子不会分页。我从url: sitename/blog/转到sitename/blog/page/2/,但没有找到任何内容,我发现错误

  

"糟糕!无法找到该页面。这可以在我的404页面找到。"

对于我的前两个循环,我使用了以下代码(使用不同的查询)

<?php query_posts('cat=2&showposts=3'); ?>

但是从研究中我发现这是错误的做法,我应该查询帖子:

$the_query = new WP_Query($query_args);

所以我已经将页面底部的查询更改为此格式,并暂时删除了前两个(错误的)查询。但是我仍然得到同样的错误。

请有人帮忙。我的PHP知识是有限的,这是拉伸我。我用来显示帖子和分页的完整代码是:

<?php 

            $query_args = array(
                'cat' => '3',
                'post_type' => 'post',
                'posts_per_page' => 6,
                'paged' => $paged
            );

            // Get current page and append to custom query parameters array
            $query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

            $the_query = new WP_Query($query_args);

            // Pagination fix
            $temp_query = $wp_query;
            $wp_query   = NULL;
            $wp_query   = $the_query;


        ?>

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

        <div class="articleThirdBlock">

                <div class="post__thumbnail post__thumbnail--marginTop">
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(400,250)); ?></a>
                </div><!-- /.post__thumbnail -->

                <a href="<?php the_permalink();?>" class="other__link"><h2 class="other__title"><?php the_title(); ?></h2></a>


        </div><!-- /.articleThirdBlock -->  

            <?php endwhile; endif; ?>

            <?php wp_reset_postdata(); 

            // Custom query loop pagination
            previous_posts_link( 'Older Posts' );
            next_posts_link( 'Newer Posts', $custom_query->max_num_pages );

            // Reset main query object
            $wp_query = NULL;
            $wp_query = $temp_query;


            ?>

1 个答案:

答案 0 :(得分:1)

自定义分页如«prev 1 2 3 next»

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

$args = array(
    'post_type'=>'post', // Your post type name
    'posts_per_page' => 6,
    'paged' => $paged,
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
        <div class="articleThirdBlock">
            <div class="post__thumbnail post__thumbnail--marginTop">
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(400,250)); ?></a>
            </div><!-- /.post__thumbnail -->
            <a href="<?php the_permalink();?>" class="other__link"><h2 class="other__title"><?php the_title(); ?></h2></a>
        </div><!-- /.articleThirdBlock -->
    <?php
    endwhile;

    $total_pages = $loop->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }    
}
wp_reset_postdata();
?>