2011-12-30 166 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> 
相關問題