2014-09-01 133 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