0

我正在为我正在开发的网站构建自定义站点地图,我设法应用自定义过滤器并显示正确页面的唯一方法是创建几个循环,每次检查父母是否有孩子,然后相应地显示它们. 这是一个相当大的网站,内容丰富。

这是我当前的循环,它绘制出每个页面标题和链接:

<?php $children = get_posts( array('category' => $region_id, 'post_type' => 'page', 'numberposts' => 100, 'post_parent' => $pg->ID, 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>

<?php if ($children) : ?>
    <ul class="children">
        <?php foreach ($children as $child) : ?>
            <?php $subkids = get_posts( array('category' => $region_id, 'post_type' => 'page', 'numberposts' => 100, 'post_parent' => $child->ID, 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
            <li>
                <a href="<?php echo get_permalink($child->ID); ?>" title="<?php echo $child->post_title;?>"><?php echo $child->post_title;?></a>
                <?php if( count( $subkids ) != 0 ) : ?>
                    <ul class="children">
                        <?php foreach($subkids as $kid) : ?>
                            <?php $subkids2 = get_posts( array('category' => $region_id, 'post_type' => 'page', 'numberposts' => 100, 'post_parent' => $kid->ID, 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
                            <li>
                                <a href="<?php echo get_permalink($kid->ID); ?>" title="<?php echo $kid->post_title; ?>"><?php echo $kid->post_title; ?></a>
                                <?php if( count( $subkids2 ) != 0 ) : ?>
                                    <ul class="children">
                                        <?php foreach($subkids2 as $kid2) : ?>
                                            <?php $subkids3 = get_posts( array('category' => $region_id, 'post_type' => 'page', 'numberposts' => 100, 'post_parent' => $kid2->ID, 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
                                            <li>
                                                <a href="<?php echo get_permalink($kid2->ID); ?>" title="<?php echo $kid2->post_title; ?>"><?php echo $kid2->post_title; ?></a>
                                                <?php if( count( $subkids3 ) != 0 ) : ?>
                                                    <ul class="children">
                                                        <?php foreach($subkids3 as $kid3) : ?>
                                                            <li>
                                                                <a href="<?php echo get_permalink($kid3->ID); ?>" title="<?php echo $kid3->post_title; ?>"><?php echo $kid3->post_title; ?></a>
                                                            </li>
                                                        <?php endforeach; ?>
                                                    </ul>
                                                <?php endif; //for if forth level children exist ($subkid3) ?>
                                            </li>
                                        <?php endforeach; ?>
                                    </ul>
                                <?php endif; //for if third level children exist ($subkid2) ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; //for if second level children exist ($subkids) ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

编辑:我从代码中删除了自定义元过滤器以减少空间,但这是我在每个循环中应用的示例:

<?php $meta = get_post_meta($child -> ID, 'wpcf-visible-in-sitemap'); ?>
<?php if($meta[0] != 1) : // If page should not be displayed, hide it ?>
    <li><a href="<?php echo get_permalink($child -> ID); ?>" alt="<?php echo get_the_title($child -> ID); ?>"><?php echo get_the_title($child -> ID); ?></a></li>
<?php endif; ?>

这看起来真的很讨厌,有没有更有效的编码方式?

4

1 回答 1

1

递归函数解决了我的问题,我能够将代码简化为以下内容:

function check_for_children( $region_id, $children ) {
    if ( count($children) > 0 ) {
        echo '<ul class="children">';
        foreach($children as $child) {
            echo '<li>' . $child -> post_title . '</li>';
            $children = get_posts( array('category' => $region_id, 'post_type' => 'page', 'numberposts' => 100, 'post_parent' => $child -> ID, 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC') );
            check_for_children($region_id, $children);
        }
        echo '</ul>';
    } else {
        return;
    }
}

该函数会调用自身,直到不再有 和 孩子,此时它会移动到下一组。

于 2013-08-20T20:47:22.490 回答