2013-10-17 22 views
0

我發現這個優秀的功能,以顯示在特定的自定義分類下列出的所有帖子。它效果很好。在這裏找到http://gilbert.pellegrom.me/wordpress-list-posts-by-taxonomy我嘗試了一些想法,但不成功,嘗試和分頁返回的數據。我要麼沒有數據,要麼繼續顯示整個列表。我的一些分類法有超過10K個相關的帖子。所以分頁看起來合乎邏輯。WordPress的分類列表 - 分頁

我想要做的是;有返回的信息創建「n」個帖子的頁面併爲其他頁面創建鏈接(1,2,... 4,5等)。任何幫助是極大的讚賞。

我在我的函數文件中扔了這個;

function list_posts_by_taxonomy($post_type, $taxonomy, $get_terms_args = array(), 
    $wp_query_args = array()){ 
    $tax_terms = get_terms($taxonomy, $get_terms_args); 

    if($tax_terms){ 
    foreach($tax_terms as $tax_term){ 
     $query_args = array(
      'post_type' => $post_type, 
      "$taxonomy" => $tax_term->slug, 
      'post_status' => 'publish', 
      'posts_per_page' => -1, 
      'ignore_sticky_posts' => true 
     ); 
     $query_args = wp_parse_args($wp_query_args, $query_args); 

     $my_query = new WP_Query($query_args); 
     if($my_query->have_posts()) { ?> 
      <h2 id="<?php echo $tax_term->slug; ?>" class="title"> 
      <?php echo $tax_term->name; ?></h2> 

      <ul> 
      <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 

       <li><a href="<?php the_permalink() ?>" rel="bookmark" 
        title="Permanent Link to <?php the_title_attribute(); ?>"> 
        <?php the_title(); ?></a></li> 

      <?php endwhile; ?> 
      </ul> 
      <?php 
     } 
     wp_reset_query(); 
    } 
} 
} 

?> 

而這種代碼放在模板,任何東西「分類」的名字,並顯示數據。另一個問題我不確定,如果分頁應該放在函數或模板中。

<div class="my_class"> 
<?php 
list_posts_by_taxonomy('my_posttype', 'taxo_mytaxo'); 
?> 
</div> 

謝謝大家!

回答

1

爲了讓您的查詢分頁首先您必須使用paged參數。
爲了得到一些額外的信息檢查WordPress的抄本爲Pagination

通常會得到這樣的分頁變量:通過將其包含在查詢參數爲您的代碼

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?> 

然後將它傳遞給查詢:

$query_args = array(
      'post_type'   => $post_type, 
      "$taxonomy"   => $tax_term->slug, 
      'post_status'   => 'publish', 
      'posts_per_page'  => -1, 
      'ignore_sticky_posts' => true 
      'paged'    => $paged //I've added it here 
     ); 

那麼你就必須建立分頁鏈接類似(這將在循環內進行):

<!-- Add the pagination functions here. --> 

<div class="nav-previous alignleft"><?php next_posts_link('Older posts'); ?></div> 
<div class="nav-next alignright"><?php previous_posts_link('Newer posts'); ?></div>