dongqiang8058 2014-09-01 05:46 采纳率: 100%
浏览 14
已采纳

破碎的分页

I've been slamming my head against a wall trying to figure this out but can't seem to get a solution. I have offset defined as 0 but in my pagination links offset+1 for older posts just leads to an error page. Not really sure where I'm going wrong. Any help would be appreciated.

Here is all the code from my page.php file...

<?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++; ?>

And then my pagination links

<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>

Thanks in advance for any help anyone can provide

  • 写回答

1条回答 默认 最新

  • duanlu7680 2014-09-01 06:30
    关注

    Its not entirely clear on what you are trying to achieve, but I think you want to look closely at http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

    Specifying hard-coded offsets in queries can and will break pagination since offset is used by WordPress internally to calculate and handle pagination.

    To get around this limitation, you will need to write some additional code to manually handle pagination; you need to detect whether a loop has additional pages and then dynamically calculate the appropriate offset for the current page.

    The code for controlling custom pagination will all occur within your functions.php file and not within the template page.php You can set an initial offset, as well as redefine the number of posts per page. There are specific samples displayed on the codex link above.

    You will be adding the action before the query is run, via

       add_action('pre_get_posts', 'myprefix_query_offset', 1 );
    

    and you will have to account for the customization via

       add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂