получить страницы - Отображать страницы ребенка, а затем внука


У меня такая структура страницы.

    Portfolio
    - Residential
    - - Resident 1
    - - Resident 2
    - - Resident 3
    - Commercial
    - - Commercial 1
    - - Commercial 2
    - - Commercial 3

На страницах внука Резидента 1 - Реклама 3 есть миниатюры сообщений

У меня есть шаблон для страницы портфолио, где я хотел бы отобразить "Жилые" и "Коммерческие" страницы с их дочерними страницами примерно так.

    <div class="grid">
      <h2>Residential</h2>
      <ul>
        <li>  Resident 1 post_thumbnail </li>
        <li>  Resident 2 post_thumbnail </li>
        <li>  Resident 3 post_thumbnail </li>
      </ul>
    </div>
    <div class="grid">
      <h2>Commercial</h2>
      <ul>
        <li>  Commercial 1 post_thumbnail </li>
        <li>  Commercial 2 post_thumbnail </li>
        <li>  Commercial 3 post_thumbnail </li>
      </ul>
    </div>

Я использую get_pages, и у меня есть div, созданные для "жилых" и "коммерческих"

    <?php

        $portfolio_sections = array(
            'post_type'     => 'page',
            'child_of'      => $post->ID,
            'sort_column' => 'menu_order',
            'sort_order'    => 'ASC',
            'parent'            => $post->ID
        );

        $sections = get_pages($portfolio_sections);

        foreach($sections as $section){

        ?>

        <div class="grid">
            <h2><?php echo $section->post_title; ?></h2>

              //Create Child pages

        </div><!--.grid-->

        <?php   
        }
    ?>

Моя проблема заключается в создании дочерних страниц в списке "ul"

Я попытался использовать второй цикл foreach, но это не сработало, и я не знаю, правильно ли это

    <?php

        $portfolio_sections = array(
            'post_type'     => 'page',
            'child_of'      => $post->ID,
            'sort_column' => 'menu_order',
            'sort_order'    => 'ASC',
            'parent'            => $post->ID
        );

        $sections = get_pages($portfolio_sections);

        foreach($sections as $section){

        ?>

        <div class="grid">
            <h2><?php echo $section->post_title; ?></h2>
            <ul class="imageGrid">
                <?php
                    $portfolio_pages = array(
                        'post_type'     => 'page',
                        'child_of'      => $section->ID,
                        'sort_column' => 'menu_order',
                        'sort_order'    => 'ASC',
                        'parent'            => $section->ID
                    );

                    $pages = get_pages($portfolio_pages);

                    foreach($pages as $page){
                        ?>
                            <li><a href="<?php echo get_the_permalink($page->ID);?>"><?php echo get_the_post_thumbnail($page->ID, "thumbnail"); ?><span><?php echo get_the_title($page->ID);?></span></a></li>
                        <?php
                    }
                ?>
            </ul>
        </div><!--.grid-->

        <?php   
        }
    ?>

--- ОБНОВЛЕНИЕ----

Структура, которую я хотел. Разделитель вокруг каждого списка ul

    <div class="grid">
      <h2>Residential</h2>
      <ul>
        <li>  Resident 1 post_thumbnail </li>
        <li>  Resident 2 post_thumbnail </li>
        <li>  Resident 3 post_thumbnail </li>
      </ul>
    </div>
    <div class="grid">
      <h2>Commercial</h2>
      <ul>
        <li>  Commercial 1 post_thumbnail </li>
        <li>  Commercial 2 post_thumbnail </li>
        <li>  Commercial 3 post_thumbnail </li>
      </ul>
    </div>

Структура из кода представляет собой один окружающий раздел.

    <div class="grid">
      <h2>Residential</h2>
      <ul>
        <li>  Resident 1 post_thumbnail </li>
        <li>  Resident 2 post_thumbnail </li>
        <li>  Resident 3 post_thumbnail </li>
      </ul>
      <h2>Commercial</h2>
      <ul>
        <li>  Commercial 1 post_thumbnail </li>
        <li>  Commercial 2 post_thumbnail </li>
        <li>  Commercial 3 post_thumbnail </li>
      </ul>
    </div>
Author: Simon Cooper, 2013-09-07

1 answers

Вам не нужно 2 запроса, просто используйте немного логики и 2 цикла для каждого:

$portfolioID = $post->ID;

$portfolio_sections = array(
  'post_type' => 'page',
  'child_of' => $portfolioID,
  'sort_column' => 'menu_order',
  'sort_order' => 'ASC',
);

$sections = get_pages($portfolio_sections);

$hierachical = array();

if ( ! empty($sections) ) {
  foreach ( $sections as $section ) {
    if ( $section->post_parent == $portfolioID ) {
      if ( ! isset( $hierachical[$section->ID]) ) $hierachical[$section->ID] = array();  
      $hierachical[$section->ID]['child'] = $section;
      $hierachical[$section->ID]['grandchildes'] = array();
    } else {
      if ( ! isset( $hierachical[$section->post_parent]) ) $hierachical[$section->post_parent] = array();
      $hierachical[$section->post_parent]['grandchildes'][] = $section;
    }
  }
  foreach ( $hierachical as $id => $hierachical_data ) {
    if ( ! isset($hierachical_data['child']) || ! is_object($hierachical_data['child']) ) continue;
    echo '<div class="grid">';
    echo '<h2>' . get_the_title($hierachical_data['child']->ID) . '</h2>';
    echo '<ul>';
    if ( isset($hierachical_data['grandchildes']) && ! empty($hierachical_data['grandchildes']) ) {
      foreach ( $hierachical_data['grandchildes'] as $grandchild ) {
        if ( has_post_thumbnail($grandchild->ID)) {
          echo '<li><a href="' . get_permalink( $grandchild->ID ) . '" title="' . esc_attr( $grandchild->post_title ) . '">';
          echo get_the_post_thumbnail($grandchild->ID);
          echo '</a></li>';
        }
      }
    }
    echo '</ul>';
    echo '</div>';
  }
}

Код непроверенный , но должен работать.

 1
Author: gmazzap, 2013-09-07 12:57:13