問題描述
還有一些其他的問題 (WP_Query 分頁似乎是很多人的一個巨大的問題),所以我正在努力縮小到如何使它的功能。
我可以建立一個單獨的自定義迴圈與分頁這個程式碼:
// http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$args = array(
'showposts' => 2,
'paged' => $paged
);
$wp_query->query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
// The Post
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
// http://codex.wordpress.org/Function_Reference/paginate_links#Examples
$big = 999999999;
$pag_args = array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
);
echo paginate_links($pag_args);
$wp_query = null;
$wp_query = $temp;
… 但是,如果我複製/貼上這個迴圈,它自然就可以作為一個確切的克隆,這意味著當你點選 「第 2 頁」 時,你需要你的迴圈。
有沒有辦法打破這些分開,所以每個分頁?
如果任何人有興趣設定自己的本地版本並使用它來修復,這裡是完整的程式碼:重複迴圈:http://paste.pocoo.org/show/573108/
最佳解決方案
是的,可以做到。關鍵是使兩個查詢的 format 引數不同:
<!-- Cats -->
<div class="animals">
<?
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => $paged1,
'posts_per_page' => 2,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
// http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $query1->max_num_pages,
'add_args' => array( 'paged2' => $paged2 )
);
echo paginate_links( $pag_args1 );
?>
</div>
<!-- Dogs -->
<div class="animals">
<?
// Custom Loop with Pagination 2
$args2 = array(
'paged' => $paged2,
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。