2014-03-25 53 views
0

我有一個自定義帖子類型稱爲標準(標準)。我正在嘗試構建一個每頁最多12個帖子的歸檔頁面。我很難讓導航/分頁顯示出來。自定義帖子類型導航導航

<section class='no-padding-top border-bottom triple-margin-bottom triple-padding-bottom'> 
<div class='container'> 
    <div class='row'> 
    <div class='col-md-1'></div> 
    <div class='col-md-10 text-center'> 
    <?php 
    $args = array(
     'post_type' => 'the-standard', 
     'orderby' => 'asc', 
     'posts_per_page' => 12, 
     'paged' => $paged, 
    ); 
     $issues = new WP_Query($args); 
     if($issues->have_posts()) { 
     while($issues->have_posts()) { 
     $issues->the_post(); 
     ?> 
     <p><?php the_date();?><br /> 
     <a href="<?php the_permalink(); ?>"><span class='no-margin bold-font-name'><?php the_title(); ?></span> - <?php the_field('page-subheader'); ?></a>    
     </p> 
     <?php 
      } 
     } 
     else { 
      echo 'Nothing to see here. Keep moving.'; 
     } 
     ?> 
     <p class='pull-left'><?php previous_posts_link('Next'); ?></p> 
     <p class='pull-right'><?php next_posts_link('Previous'); ?></p> 
    </div> 
    </div class="col-md-1"></div> 
    </div> 
</div> 
</section> 
+1

不是在頁面模板中創建新的WP_Query,而是嘗試爲該帖子類型創建存檔模板(將其命名爲「archive-the-standard.php」並修改該帖子類型的現有存檔查詢,而不是「將您的擁有」)。這使得分頁更容易 – Ennui

+0

看着'pre_get_posts'動作鉤子和'$ query-> set'函數來修改主查詢 – Ennui

+0

Ennui,謝謝你的提示。你知道在哪裏可以找到一些有關使用適當的查詢設置歸檔頁面的文檔嗎? – mattfiedler

回答

0

試試這個:

function modify_cpt_archive($query) { 
    if ($query->is_main_query() && !is_admin() && is_post_type_archive('the-standard')) { 
     $query->set('post_type', 'the-standard'); 
     $query->set('order', 'ASC'); 
     $query->set('posts_per_page', '12'); 
    } 
    return $query; 
} 
add_action('pre_get_posts', 'modify_cpt_archive'); 

添加到您的主題(或子主題)functions.php文件。

該函數使用pre_get_posts鉤子,該鉤子在創建查詢對象之後但在運行實際查詢之前調用它,從而允許您修改默認查詢。條件語句確保它隻影響前端自定義發佈類型的歸檔頁面(否則會影響所有默認查詢),然後修改所需的查詢參數並返回更新後的查詢對象。