2016-07-06 17 views
0

我已經閱讀了許多其他像我的問題,但無法找到修復程序。出於某種原因,我的自定義查詢不分頁,我無法解決原因。這可能是一個愚蠢的理由,但希望你能提供幫助。WP_Query分頁不能在自定義帖子類型上使用自定義查詢

這是我的代碼。

<?php 
      global $wp_query; 
      $title = $wp_query->post->post_title; 
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
      $query = new WP_Query(
       array(
        'paged' => $paged, 
        'wpse18703_title' => "$title", 
        'posts_per_page' => 10, 
        'post__not_in' => array($post->ID), 
        'post_type' => array('post', 'features'), 

       ) 
      ); 

      if ($query->have_posts()){ 
       while ($query->have_posts()) { $query->the_post(); ?> 
        <a href="<?php echo get_permalink(); ?>"> 
        <div class="small-12 large-12 gdb-news-container"> 
        <div class="small-12 large-4 columns gdb-news-image"> 
         <?php the_post_thumbnail('game-news-thumb'); ?> 
        </div> 
        <div class="small-12 large-8 columns game-title"> 
         <h5><?php the_title(); ?></h5> 
        <?php echo content(20); ?> 
        </div> 
         <div class="clearfix"></div> 
        </div> 
        </a> 

     <?php 
       } //end while 
       wp_reset_query(); 
      } //end if 
      ?> 

      <?php if (function_exists('reverie_pagination')) { reverie_pagination(); } else if (is_paged()) { ?> 
       <nav id="post-nav"> 
        <div class="post-previous"><?php next_posts_link(__('&larr; Older posts', 'reverie')); ?></div> 
        <div class="post-next"><?php previous_posts_link(__('Newer posts &rarr;', 'reverie')); ?></div> 
       </nav> 
      <?php } ?> 

回答

0
<?php 
    if(isset($_GET['paged']) && !empty($_GET['paged'])):endif; 
    global $wp_query; 
    $temp = $wp_query; 
    $wp_query = null; 
    $title = $wp_query->post->post_title; 
    $args = array(
     'paged' => $paged, 
     'wpse18703_title' => "$title", 
     'posts_per_page' => 10, 
     'post__not_in' => array($post->ID), 
     'post_type' => array('post', 'features') 
    ); 
    $wp_query = new WP_Query($args); 
    if($wp_query->have_posts()){ 
     while($wp_query->have_posts()){ 
      $wp_query->the_post(); ?> 
      <a href="<?php echo get_permalink(); ?>"> 
       <span class="small-12 large-12 gdb-news-container"> 
        <span class="small-12 large-4 columns gdb-news-image"> 
         <?php the_post_thumbnail('game-news-thumb'); ?> 
        </span> 
        <span class="small-12 large-8 columns game-title"> 
         <h5><?php the_title(); ?></h5> 
         <?php echo content(20); ?> 
        </span> 
       </span> 
      </a> 
     <?php } 
    } 
    $wp_query = null; 
    $wp_query = $temp; 
    if(function_exists('reverie_pagination')){reverie_pagination();} else if (is_paged()){ ?> 
     <nav id="post-nav"> 
      <div class="post-previous"><?php next_posts_link(__('&larr; Older posts', 'reverie')); ?></div> 
      <div class="post-next"><?php previous_posts_link(__('Newer posts &rarr;', 'reverie')); ?></div> 
     </nav><?php 
    } 
?> 

我改變了你的div與類「小12」跨越的,因爲它是無效的W3C的代碼放在一個div錨標籤內,所以你可能需要修改你的CSS文件,根據如何編碼。

+0

感謝您的代碼,但我已經嘗試過,仍然沒有分頁。我真的無法繞過我的頭,爲什麼它不工作。 – Kyon147

0

我認爲你正在使用你的$paged變量錯誤(我猜你在「頁面」而不是「帖子」中使用它)。這應該在頁面和帖子上工作:

if (get_query_var('paged')) { 
    $paged = absint(get_query_var('paged')); 
} elseif (get_query_var('page')) { 
    $paged = absint(get_query_var('page')); 
} else { 
    $paged = 1; 
} 
+0

是的這也沒有工作,我似乎無法解決爲什麼分頁不起作用。它現在正在顯示「頁面」,但頁面的鏈接只是進入同一頁面。 – Kyon147

相關問題