2015-08-25 97 views
3

今天早上我做了很多研究,找不到解決潛在的大問題的解決方案。我試圖使用wordpress分頁與自定義查詢,似乎無法讓它工作!使用WP_Query進行分頁

這是我的代碼,分頁根本不顯示。

<?php //Template Name: Acapellas ?> 
<?php get_header(); ?> 
<div id="main-content"> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-3"> 
       <?php get_sidebar('primary-left'); ?> 
      </div> 
      <div class="col-md-9"> 
       <?php get_template_part('includes/ads', 'top'); ?> 
       <div class="post-content"> 
        <h1>Free Acapellas</h1> 

        <?php if (pmpro_hasMembershipLevel(array(1,2), $user_id)) { ?> 
        <?php echo do_shortcode('[collections]'); ?> 
        <?php } ?> 

        <?php 
        $args = array (
          'post_type'    => array('acapella'), 
          'pagination'    => true, 
          'posts_per_page'   => '12', 
          'ignore_sticky_posts' => true, 
         ); 

         // The Query 
         $query = new WP_Query($args); 

        ?> 
        <?php if ($query->have_posts()) : ?> 
        <ul class="acapellas row"> 

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

         <?php get_template_part('includes/list', 'acapella'); ?> 

         <?php endwhile; ?> 
        </ul> 

        <?php // Previous/next page navigation. 
         the_posts_pagination(array(
          'prev_text'   => __('Previous page', ''), 
          'next_text'   => __('Next page', ''), 
          'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', '') . ' </span>', 
         )); 

         // If no content, include the "No posts found" template. 
         else : 
          get_template_part('content', 'none'); 

         endif; 
         ?> 

      </div> 
     </div> 
    </div> 
</div> 
<?php get_footer(); ?> 
+0

'the_posts_pagination()'不適用於自定義查詢,也不可過濾。你最好編寫自己的定製功能。你也可以破解主查詢,但我真的很討厭這樣做。 –

回答

1

只需下載並安裝插件wp-pagenavi,然後使用:

if(method_exists('wp_pagenavi')){ 
    wp_pagenavi(array('query' => $query)); 
} 

通過你的查詢對象在wp_pagenavi方法參數。

-1

我猜,你正在尋找的自定義查詢編號分頁,比嘗試本文

Kvcodes

這裏是代碼。

function kvcodes_pagination_fn($pages = '', $range = 2){ 
$showitems = ($range * 2)+1;  // This is the items range, that we can pass it as parameter depending on your necessary. 

global $paged; // Global variable to catch the page counts 
if(empty($paged)) $paged = 1; 

if($pages == '') { // paged is not defined than its first page. just assign it first page.  
    global $wp_query; 
    $pages = $wp_query->max_num_pages; 
    if(!$pages) 
     $pages = 1; 
} 

if(1 != $pages) { //For other pages, make the pagination work on other page queries  
    echo "<div class='kvc_pagination'>"; 
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>"; 
    if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>"; 

    for ($i=1; $i <= $pages; $i++) { 
     if (1 != $pages &&(!($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems)) 
      echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>"; 
    } 

    if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>"; 
    if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>"; 
    echo "</div>\n"; 
} 
} 

發生在您當前的主題功能,functions.php的

,並用它在loop.php或index.php文件

kvcodes_pagination_fn(); 

,爲WP_Query例如

$custom_query = new WP_Query("post_type=receipes&author=kvcodes"); 
while ($custom_query->have_posts()) : $custom_query->the_post(); 
// Show loop content... 
endwhile; 

kvcodes_pagination_fn($custom_query->max_num_pages); 

就是這樣。

+0

請添加一個真實答案 –

+0

@ Pieter Goosen - 從博客中複製相同的代碼沒什麼意義,只是想一想,爲什麼我們一次又一次地在一個程序上重寫一個函數。爲什麼我們現在使用CDN,我們是否每次都克隆。 – Varadha