2014-01-17 82 views
2

類別環在WordPress主題的category.php,您有以下循環:添加偏移在WordPress

if (have_posts()) : while (have_posts()) : the_post(); 
// output posts 
endwhile; endif; 

你怎麼去abouts輸出這個完全相同的循環,但與偏移?我發現,你可以做一個

query_posts('offset=4'); 

改變循環但這重置整個循環和偏移的作品,但顯示了從每個類別中所有的職位,所以我得到的印象query_posts完全復位循環,不會它只有你添加的過濾器。有沒有辦法告訴循環:

「做的正是你在做什麼,除了偏移使它4」

這可能嗎?

謝謝!

+0

爲您的'query_posts'參數提供原始代碼 –

+0

沒有,WordPress加載category.php,那就是循環(沒有query_posts()行) – user28240

+0

您必須在'query_posts'提供類別id,像'cat = 3' –

回答

5

首先不使用query_posts()see here改用WP_Query

試試這個:

//To retrieve current category id dynamically 
$current_cat = get_the_category(); 
$cat_ID = $current_cat[0]->cat_ID; 

$loop = new WP_Query(array(
    'offset' => 4,   //Set your offset 
    'cat' => $cat_ID,  //The category id 
)); 

if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); 
// output posts 
endwhile; endif; 

是爲WordPress的說:

設置偏移參數覆蓋/忽略分頁參數和 休息分頁(Click here for a workaround)

只要按照分頁解決方法說明,你就可以走了。

+0

這是行不通的,但打破了分類的分頁,我認爲這是因爲分頁僅適用於「query_posts」類型的查詢。有沒有辦法使用WP_query,但仍然有工作分頁? – user28240

+0

@ user28240它不是WP_Query的問題。設置'offset'參數會破壞這兩種方法的分頁。看我的編輯! –

+0

AH難怪,那是完美的,感謝那篇文章的鏈接! – user28240