2011-12-30 137 views
1

我有一个页面(不是文章)与n个子页面。 在主页面中,我需要显示最多3个子页面的标题,并为另一个页面插入分页。WordPress的自定义循环添加分页显示子页面

我该怎么做?

这是我现在简单的代码:

<?php 
    $parent_id = 14; //main page id 
    $pages = get_pages(array('sort_column' => 'menu_order', 'numberposts' => 3, 'child_of' => $parent_id)); 
    foreach ($pages as $page) : ?> 
     <div class="item"> 
      <div class="item-title"> 
       <h2><a href="<?php echo get_permalink($page->ID); ?>"><?php echo $page->post_title; ?></a></h2> 
      </div> 
     </div> 
    <?php endforeach; ?> 

感谢。

+0

你几乎已经明白了。但是,为了对get_pages函数的结果进行微调,我建议您使用自定义查询来获取ID大于当前子页面的1个子页面,该子页面将成为“下一个链接”,而带ID的子页面少于当前将是'prev链接'。 – Sterex 2011-12-30 14:30:59

回答

3

我自己解决,解决方案是使用wp_query()创建一个新的循环insted的使用get_pages的( )

这里是网页标题和contentwith分页从Avigma技术的新代码由Preeti杜阿:

<?php 

    // Pagination variable 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    // The Query 
    $the_query = new WP_Query(array('post_parent' => 782, 'post_type' => 'page', 'paged' => $paged)); 

    // The Loop 
    if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); 
    global $post; 
    $thePostID = $post->ID; /* this variabled is used if you need to get custom fields of the subpage */ 
     ?> 
    <div id="side-post-title"> 
     <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
    </div> 

    <div id="side-post-excerpt"> 
       <?php the_excerpt(); ?> 

      <a href="<?php echo get_permalink($page->ID); ?>"> <div id="read-more"> 
         <img src="/wp-content/uploads/2012/10/read-more-btn.png"/></div> </a>               
     </div> 



<?php endwhile; endif; ?> 

<nav class="navigation"> 
    <div style="float:left;"> <?php next_posts_link('Show older', $the_query->max_num_pages) ?></div> 
    <div style="float:right;"> <?php previous_posts_link('Show newer') ?></div> 
</nav> 
相关问题