2014-09-01 129 views
0

我一直在抨击我的头靠在墙上试图弄清楚这一点,但似乎无法得到解决方案。我有偏移量定义为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> 

预先感谢任何帮助任何人都可以提供

+2

占定制只是出于好奇,你为什么不发布这个到'http:// wordpress.stackexchange.com /' – zipzit 2014-09-01 05:50:03

+0

需要更多信息,错误页面是什么?错误页面的网址是什么? – Mattigins 2014-09-01 05:50:09

+1

题外话,属于http://wordpress.stackexchange.com。 *旁注:*为什么在同一个HTML中使用单引号和双引号? – Raptor 2014-09-01 05:52:34

回答

1

它不完全清楚你想要实现什么,但我想你想仔细看看http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

在查询中指定硬编码的偏移量可能会破坏分页 ,因为WordPress内部使用偏移量来计算和处理 分页。

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

控制都将出现在您的functions.php文件,而不是page.php文件,您可以设置一个初始偏移量,以及重新定义每页的职位数在模板中的自定义分页的代码。在上面的代码链接上显示了特定的样本。

你会被添加动作查询运行之前,通过

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

,你将不得不通过

add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2); 
+0

谢谢支持 – 2014-09-01 07:01:04