2017-02-17 150 views
0

我剛剛創建了一個新的自定義頁面模板並僅調用了兩個帖子。 如何添加分頁以便至少可以鏈接兩個最新的帖子?如何將分頁添加到我的自定義頁面模板

我想這個代碼,但它不工作:

<?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    query_posts( 
     array ( 
      'post_type' => 'post', 
      'category_name' => 'news', 
      'category' => 1, 
      'posts_per_page' => 2, 
      'paged' => $paged) 
     );  
     // The Loop 
     while (have_posts()) : the_post();?> 
      <div class="news-page-content-wrapper"> 
       <div class="news-page-content"> 
        <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1> 
        <figure><?php the_post_thumbnail(); ?></figure> 
        <p><?php echo get_the_excerpt();?></p> 
        <a href="<?php the_permalink(); ?>">Read More&raquo</a> 
       </div> 
      </div> 
     <?endwhile; 
     // Reset Query 
     wp_reset_query(); 
    ?> 

任何幫助嗎?

回答

0

由於您使用的是「循環」,您應該使用內置函數來顯示分頁。

下面是一些例子給你:https://codex.wordpress.org/Pagination

我已經更新了你的代碼示例顯示默認的分頁:?

<?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    query_posts( 
     array ( 
      'post_type' => 'post', 
      'category_name' => 'news', 
      'posts_per_page' => 2, 
      'paged' => $paged) 
     );  
     // The Loop 
     while (have_posts()) : the_post(); ?> 
      <div class="news-page-content-wrapper"> 
       <div class="news-page-content"> 
        <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> 
        <figure><?php the_post_thumbnail(); ?></figure> 
        <p><?php echo get_the_excerpt(); ?></p> 
        <a href="<?php the_permalink(); ?>">Read More&raquo; </a> 
       </div> 
      </div> 
     <?php endwhile; 

     the_post_navigation(); 
     // Reset Query 
     wp_reset_query(); 
    ?> 
+0

我想這個代碼,但它不工作,以及 <?php previous_posts_link(); ?> –

+0

@SidneySousa以上的作品,我剛剛在我的一個測試網站上測試過。 – Daniel

+0

不知道到底發生了什麼不同,但我只是複製並粘貼了來自您發送的鏈接上的代碼和代碼。 –

相關問題