WordPress打破了分页

时间:2013-11-25 13:01:51

标签: php wordpress pagination

我的自定义帖子页面上的分页工作正常但是在添加了几个帖子之后它就被打破了 - 旧的帖子链接不再有用了。

请建议我该如何解决?我已经尝试过禁用插件,更改固定链接以及几乎我能在WordPress Codex上找到的任何内容。

这是我的分页查询:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'press' , 'posts_per_page' => 50 , 'paged' => $paged);
query_posts( $args );
/*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */
//query_posts ( $args );//query_posts($query_string . '&caller_get_posts=6&posts_per_page=12');
?>
<ul class="griditemleft clear">
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
        <?php if (has_post_thumbnail() ) : ?>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('category-thumbnail'); ?></a>
                <h2 class="press-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            </li>
        <?php endif; ?>
    <?php endwhile; ?>
</ul>

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

<?php else : ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

1 个答案:

答案 0 :(得分:0)

关于你想要达到的目标并不完全清楚,但我认为你想密切关注http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

在查询中指定硬编码偏移可以并且将打破分页,因为WordPress在内部使用偏移来计算和处理分页。

要解决此限制,您需要编写一些额外的代码来手动处理分页;您需要检测循环是否有其他页面,然后动态计算当前页面的适当偏移量。

用于控制自定义分页的代码将全部发生在functions.php文件中,而不是在模板page.php中。您可以设置初始偏移量,以及重新定义每页的帖子数。上面的codex链接上显示了特定的样本。

您将在运行查询之前通过

添加操作
   add_action('pre_get_posts', 'myprefix_query_offset', 1 );

,您必须通过

考虑自定义
   add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );