2015-04-03 106 views
0

我使用這個代碼來檢索的網頁在WordPress的嵌套列表:WordPress的名單嵌套頁面與網頁內容一起

<?php wp_list_pages($args); ?> 

它給了我類似這樣的輸出:

<ul> 
    <li>Page Title 1</li> 
    <li>Page Title 2 
     <ul> 
      <li>Page Title 3</li> 
      <li>Page Title 4</li> 
     </ul> 
    </li> 
    <li>Page Title 5</li> 
</ul> 

我需要這樣的輸出:

<ul> 
    <li>Page Title 1/Page Content 1</li> 
    <li>Page Title 2/Page Content 2 
     <ul> 
      <li>Page Title 3/Page Content 3</li> 
      <li>Page Title 4/Page Content 4</li> 
     </ul> 
    </li> 
    <li>Page Title 5/Page Content 5</li> 
</ul> 

回答

0

我相信你需要使用get_pages()來代替,它將返回一個頁面對象數組TS。但是,這不會給你設置子菜單所需的層次結構。你可以通過只抓取頂層項目來克服這個問題,然後遍歷每一個並檢查子頁面:

<?php 

$pages = get_pages(array(
    'sort_column' => 'menu_order', 
    'parent' => 0, 
)); 

?> 

<ul> 

<?php foreach($pages as $page): ?> 
    <?php $title = $page->post_title; ?> 
    <?php $content = apply_filters('the_content', $page->post_content); ?> 
    <?php $children = get_pages(array('child_of' => $page->ID));?> 

    <li> 

    <?php echo $title . "/" . $content; ?> 

    <?php if (count($children) != 0): ?> 

     <ul> 

     <?php foreach($children as $child): 
      <?php $title = $child->post_title; ?> 
      <?php $content = apply_filters('the_content', $child->post_content); ?> 

      <li><?php echo $title . "/" . $content; ?></li> 

     <?php endforeach; ?> 

     </ul> 

    <?php endif; ?> 

    </li> 

<?php endforeach;?> 

</ul>