2014-01-08 121 views
2

我有一个静态的页面,上面有一些内容,并且想要在静态内容下面显示博客帖子。wordpress:在静态页面上显示2篇文章+页面导航

循环应该总是只显示2个帖子,但它必须可以通过WP_PageNavi(我总是使用的一个插件)导航到较旧的帖子。

我在我的首页做了一个静态页面,并在静态内容下添加了WP_Query。问题是,它不起作用,因为它只显示最新的两篇文章。

它看起来像这样:

<!-- here goes the static content stuff --> 

<?php if(is_front_page()) { ?> 

<div class="news"> 
    <ul> 
    <?php $my_query = new WP_Query(array('post_status' => 'publish', 'post__not_in' => $current_id)); 
     while ($my_query->have_posts()) : $my_query->the_post(); ?> 

     <li class="news-post"> 
      <h2><?php the_title(); ?></h2> 
      <p><?php the_content(); ?></a></p> 
     </li> 
     <?php endwhile; ?> 
    </ul> 

    <?php if(function_exists('wp_pagenavi')) { ?> 
     <?php wp_pagenavi(array('query' => $my_query)); ?> 
    <?php } ?> 

建议大加赞赏

回答

0
<?php if(is_front_page()) { ?> 
    <div class="news"> 
     <ul> 
      <?php 
       $paged = (get_query_var('page')) ? get_query_var('page') : 1; 

       $my_query = new WP_Query('post_type=post&post_status=publish&posts_per_page=2&paged=' . $paged); 


       while ($my_query->have_posts()) : $my_query->the_post(); ?> 

       <li class="news-post"> 
        <h2><?php the_title(); ?></h2> 
        <p><?php the_content(); ?></a></p> 
       </li> 
      <?php endwhile; ?> 
     </ul> 

     <?php 
      if(function_exists('wp_pagenavi')): 
       wp_pagenavi(array('query' =>$my_query)); 
       endif; 
     ?> 
    </div> 
<?php } ?> 

enter image description here

+0

谢谢,但PageNavi不起作用,它不会导航到其他职位,但停留在显示相同帖子的相同页面。 – okiedokey

+0

是的,它的工作原理。你添加了$ paged部分,对吧? – okiedokey

+0

是的,我在我的网站测试后更改了这段代码。所以工作表示感谢。 –

相关问题