【问题标题】:Broken Pagination分页损坏
【发布时间】:2014-09-01 05:46:09
【问题描述】:

我一直在用头撞墙试图解决这个问题,但似乎无法找到解决方案。我已将偏移量定义为 0,但在我的分页链接中,旧帖子的偏移量 + 1 只会导致错误页面。不太确定我哪里出错了。任何帮助将不胜感激。

这是我 page.php 文件中的所有代码...

<?php

$offset = $_GET['offset'] ? $_GET['offset'] : 0;

$page_title = $wp_query->post->post_title;
$total_posts = wp_count_posts()->publish;

if ( $page_title == "Blog" ) {

?>
<div id="blog_content">
<?php
    if($_GET['message']){
        echo "<div style='background-color:#d9ffd1; padding:10px; margin-bottom:20px;'>".stripslashes($_GET["message"])."</div>";
    }
?>

<?php
    $post_count = 0;

    $args = array( 'numberposts' => 10, 'post_status' => "publish", "offset"=>$offset*10);
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); $post_count++; ?>

然后是我的分页链接

<div style="font-size:12px;">
        <div style="float:left; width:49%;">
        <?php
        the_post();

            if ($offset > 0): ?>
        <a href="<?php the_permalink()?>&offset=<?=$offset-1?>">&larr; Newer Posts</a>
        <?php endif; ?>
        </div>
        <div style="float:right; width:49%; text-align: right;">
        <?php
        $next_post = get_next_post();
        if ($total_posts > $post_count + ($offset*10)): ?>
        <a href="<?php the_permalink()?>&offset=<?=$offset+1?>">Older Posts &rarr;</a>
        <?php endif; ?>
        </div>
        <div style="clear:both;"></div>
    </div>

提前感谢任何人都可以提供的任何帮助

【问题讨论】:

  • 只是出于好奇,你为什么不把这个发到http://wordpress.stackexchange.com/
  • 需要更多信息,错误页面是什么?错误页面的url是什么?
  • 离题,属于wordpress.stackexchange.com旁注: 为什么在同一个 HTML 中使用单引号和双引号?
  • 您是否仔细查看了 pre_get_posts() 和 found_posts() 函数调用?查看http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
  • 这句话是什么意思? I have offset defined as 0 but in my pagination links offset+1 for older posts just leads to an error page. 究竟是什么,你想达到什么目的? (offset = zero 不是这里的默认行为吗?get_option('posts_per_page'); 不是更容易工作吗?

标签: php wordpress pagination


【解决方案1】:

您要达到的目标尚不完全清楚,但我认为您想仔细查看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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    相关资源
    最近更新 更多