2016-02-12 52 views
1

所以基本上我想循環出3個職位,並添加下一個/上一頁按鈕來回來列出3新/舊每次。然而,這並不像預期的那樣工作,我拉着頭髮試圖弄清楚什麼是錯的。WordPress的get_next_posts循環

發生什麼是我得到一個「下一步」按鈕,我可以點擊它,它將我導航到/ page/2,但顯示相同的3個結果和「Previous」按鈕不顯示。我哪裏錯了? :(

<?php 

     /* 
      Template Name: News Listing 
     */ 
    ?> 
    <div id="news-list"> 
     <h1>Wow thats great <span>news</span></h1> 
     <div class="handwritten">Uncensored and timely truth from the brewery</div> 
     <div class="news-container"> 
      <?php 
       // set up or arguments for our custom query 
       $paged = (get_query_var('page')) ? get_query_var('page') : 1; 
       $query_args = array(
        'post_type' => 'post', 
        'category_name' => '', 
        'posts_per_page' => 3, 
        'paged' => $paged 
       ); 
       // create a new instance of WP_Query 
       $the_query = new WP_Query($query_args); 
      ?> 

      <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); // run the loop ?> 
      <div class="news-post"> 
       <?php 
        $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
       ?> 
       <img src="<?= $feat_image; ?>" /> 

       <h2><?php echo the_title(); ?></h2> 

       <?php the_excerpt(); ?> 

       <a href="#">Read more</a> 
       <p class="published-date">Posted <?php the_time('d F');?></p> 
      </div> 
      <?php endwhile; ?> 
      <div class="clearfix"></div> 
     </div> 
     <?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?> 
     <div class="next-prev"> 
      <div class="link prev"> 
       <?php echo get_previous_posts_link('<span class="handwritten">Next post</span><h4>That way</h4>'); // display older posts link ?> 

      </div> 

      <div class="link next"> 
       <?php echo get_next_posts_link('<span class="handwritten">Next post</span><h4>This way</h4>', $the_query->max_num_pages); // display older posts link ?> 
      </div> 
     </div> 
     <?php } ?> 

     <?php else: ?> 
     <article> 
      <h1>Sorry...</h1> 
      <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
     </article> 
     <?php endif; ?> 

     <div class="clearfix"></div> 
    </div> 
+0

當然,你有檢查'$ paged'實際包含的值是多少,對嗎? – CBroe

+0

@CBroe你是什麼意思?當我回顯$ paged時,我什麼都沒有收到 – Codehiker

+0

@CBroe好的,你指出我的方向是正確的如果頁面不是默認的靜態主頁你應該使用get_query_var('paged')來代替get_query_var('page')頁面 – Codehiker

回答

0

的問題是與不是我的模板標準的靜態主頁,它是一個自定義的。

爲此我需要使用的get_query_var('paged')代替get_query_var('page')