dslk6326846 2013-08-20 19:01
浏览 40
已采纳

在构建WordPress自定义站点地图时避免循环中的循环

I am building a custom sitemap for a website I am working on, the only way I've managed to apply my custom filters and display the proper pages is to create several loops that check each time if the parent has children, then displays them accordingly. This is a fairly large site and has a ton of content.

This is my current loop which draws out each page title and link:

<?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; ?>

Edit: I removed my custom meta filters from the code to reduce space, but here is an example of what I apply inside each loop:

<?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; ?>

This is really nasty looking, is there a more efficient way of coding this?

  • 写回答

1条回答 默认 最新

  • doupa1883 2013-08-20 20:47
    关注

    Recursive functions have resolved my issue, I was able to reduce the code to the following:

    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;
        }
    }
    

    The function calls itself until there are no longer and children, at which point it moves along to the next set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?