2017-02-17 75 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

不知道到底发生了什么不同,但我只是复制并粘贴了来自您发送的链接上的代码和代码。 –

相关问题