使用ACF在循环中中断分页

时间:2014-03-10 作者:davehudson52

http://dthudson.com/ccr/archive-issues/

分页的页面由您在底部(帖子下方)看到的内容填充,但当您单击“2”时,它会加载第2页,但显示完全相同的帖子。我想指出,我正在使用高级自定义字段从帖子类型“issues”中检索内容,我想知道这是否是问题的根源。另外,我从索引页和搜索页复制了分页代码,每一个都很好。有什么想法吗?

<!-- BEGIN LOOP -->         

<?php 

$args = array(
        \'post_type\' => \'issues\',
        \'posts_per_page\' => \'9\',
        \'order\' => \'ASC\',
        \'orderby\' => \'meta_value_num\'
);

$my_query = new WP_Query( $args );


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

            <div class="loop four columns hover-issue">
                <div class="column-nest">
                    <a class="hover-color" href="<?php echo get_permalink(); ?>"><h5 class="staff-title big-top-space"><?php the_field(\'release_season\'); ?> <?php the_field(\'release_year\'); ?></h5>
                    <h6 class="staff-name  bottom-space"><?php the_field(\'issue_number_full_length\'); ?></h6></a>               
                </div> <!-- column-nest -->
                <figure class="staff-photo-border">
                    <a href="<?php echo get_permalink(); ?>"><img class="column-nest image-width" src="<?php the_field(\'issue_image_cropped\'); ?>" alt="photo of issue <?php the_field(\'issue_number\'); ?>" /></a>
                </figure> <!-- staff-photo-border -->
            </div> <!-- loop four columns -->


<?php the_content(); ?>
<?php endwhile; endif; ?>

        </div> <!-- looper twelve columns -->

                    <nav class="pagination">

                        <?php

                        $total_pages = $my_query->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_next\'    => False,
                              \'type\' => \'list\', 
                            ));  
                        }  
                        ?>

       </nav><!--end pagination-->  

1 个回复
SO网友:Milo

您的查询显示前9篇文章,而不考虑页码,因为您没有在查询参数中设置页码。

$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$args = array(
    \'post_type\' => \'issues\',
    \'posts_per_page\' => \'9\',
    \'order\' => \'ASC\',
    \'paged\' => $paged
);

结束

相关推荐

Pagination for user list

我使用get_user() 作用但我需要把结果分页。我试了很多,但没有成功。下面是代码,它没有按预期工作:$args = array( \'meta_query\' => array( array( \'key\' => \'ib_s2member_custom_fields\', \'value\' => trim($_GET[\"country\"]), \'compare\' => \

使用ACF在循环中中断分页 - 小码农CODE - 行之有效找到问题解决它

使用ACF在循环中中断分页

时间:2014-03-10 作者:davehudson52

http://dthudson.com/ccr/archive-issues/

分页的页面由您在底部(帖子下方)看到的内容填充,但当您单击“2”时,它会加载第2页,但显示完全相同的帖子。我想指出,我正在使用高级自定义字段从帖子类型“issues”中检索内容,我想知道这是否是问题的根源。另外,我从索引页和搜索页复制了分页代码,每一个都很好。有什么想法吗?

<!-- BEGIN LOOP -->         

<?php 

$args = array(
        \'post_type\' => \'issues\',
        \'posts_per_page\' => \'9\',
        \'order\' => \'ASC\',
        \'orderby\' => \'meta_value_num\'
);

$my_query = new WP_Query( $args );


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

            <div class="loop four columns hover-issue">
                <div class="column-nest">
                    <a class="hover-color" href="<?php echo get_permalink(); ?>"><h5 class="staff-title big-top-space"><?php the_field(\'release_season\'); ?> <?php the_field(\'release_year\'); ?></h5>
                    <h6 class="staff-name  bottom-space"><?php the_field(\'issue_number_full_length\'); ?></h6></a>               
                </div> <!-- column-nest -->
                <figure class="staff-photo-border">
                    <a href="<?php echo get_permalink(); ?>"><img class="column-nest image-width" src="<?php the_field(\'issue_image_cropped\'); ?>" alt="photo of issue <?php the_field(\'issue_number\'); ?>" /></a>
                </figure> <!-- staff-photo-border -->
            </div> <!-- loop four columns -->


<?php the_content(); ?>
<?php endwhile; endif; ?>

        </div> <!-- looper twelve columns -->

                    <nav class="pagination">

                        <?php

                        $total_pages = $my_query->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_next\'    => False,
                              \'type\' => \'list\', 
                            ));  
                        }  
                        ?>

       </nav><!--end pagination-->  

1 个回复
SO网友:Milo

您的查询显示前9篇文章,而不考虑页码,因为您没有在查询参数中设置页码。

$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$args = array(
    \'post_type\' => \'issues\',
    \'posts_per_page\' => \'9\',
    \'order\' => \'ASC\',
    \'paged\' => $paged
);

相关推荐

Count posts for pagination

我正在为一个网站分页<;上一页(页码)下一页>很简单,已经完成。但是现在我需要添加一个选择器来直接转到页面(例如:转到第7页),要这样做,我需要知道有多少页面,为此我需要计算在查询中找到了多少帖子。问题是这个网站有太多的帖子(>13.000),查询所有帖子都会减慢页面加载速度,这就像。。。10秒后页面才能加载。显然,这是不可接受的。分页解决了这个问题,因为一次只加载50或100篇文章,但我无法将它们全部计算在内。我可以在不加载的情况下统计某个查询中的帖子吗?或者我可以通过其他方式获得页数吗