【问题标题】:Custom wordpress loop pagination not working自定义 wordpress 循环分页不起作用
【发布时间】:2016-06-03 17:01:17
【问题描述】:

我有一个网站,我在一个页面上有几个来自不同类别的循环。帖子像我想要的那样显示。在页面底部,我想显示该站点的所有帖子。我也让这个工作正常。但是帖子不会分页,当我点击“较新的帖子”时,我会从这个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;


            ?>

【问题讨论】:

    标签: php wordpress pagination


    【解决方案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();
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多