Wordpress,get_pages中的嵌套查询

时间:2013-09-08 09:39:54

标签: wordpress loops

我有这样的页面结构。

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

孙子页面Resident 1 - Commercial 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并且我为'Residential'和'Commercial'创建了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 } ?>

1 个答案:

答案 0 :(得分:0)

我相信根据get_posts的文档,您可能会遇到错误。参数child_of列出所提供ID的子页面,而parent列出仅提供单页ID作为父页面的页面。因此,通过在两个查询中提供它,您将不会在查询中生成任何结果。我建议按如下方式更新2个参数块:

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

然后在你的foreach中为每个子页面查询:

$portfolio_pages = array(
    'post_type'   => 'page',
    'sort_column' => 'menu_order',
    'sort_order'  => 'ASC',
    'parent'      => $section->ID
);