开发者

Wordpress list child pages alphabetically

开发者 https://www.devze.com 2023-03-05 03:39 出处:网络
I have a parent page with about 200 child pages which constitute a lexicon. In the template of the parent page, I would like a query have allows the following listing. I want to show the first letter

I have a parent page with about 200 child pages which constitute a lexicon. In the template of the parent page, I would like a query have allows the following listing. I want to show the first letter for every group of words that starts with that letter. If a letter is not represented in the开发者_开发百科 list, it should not be shown.

A

Albert

Allan

Amanda

B

Bill

Bob

Bruno

C

Charles

Christine

and so on....


My answer comes late for you but maybe someone will find it useful. I needed the same thing so I had to write this template:

<?php
/*
Template Name: Alphabetic
*/
?>


<?php get_header(); ?>
<div class="container">

    <div id="content" role="main">
    <h2 class="entry-title"><?php the_title(); ?></h2>                              
        <?php

        $my_wp_query = new WP_Query();
        $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

        $children = get_page_children($post->ID, $all_wp_pages);

        $letter="";
        foreach ($children as $child)
        {
            $first_letter=substr($child->post_title,0,1);
            if($letter != $first_letter)
            {
                $alphabetic[]->post_title=$first_letter;
                $letter=$first_letter;
            }
            $alphabetic[]=$child;
        }

        ?>

        <table style="border:none;">
            <tr>

            <?php

            $col = 4; //how many columns

            for($i = 0; $i < $col; $i++) {

                echo '<td style="border:none;">';

                    $nr = (int)(sizeof($alphabetic)/4);
                    $i == $col - 1 ? $end = sizeof($alphabetic) : $end = $nr*($i+1);

                    for($j = $nr*$i; $j < $end; $j++) {
                        if(strlen($alphabetic[$j]->post_title)==1)
                            echo '<b>', $alphabetic[$j]->post_title, '</b><br />';
                        else
                            echo '<a href="'.get_permalink($alphabetic[$j]->ID).'">'.$alphabetic[$j]->post_title.'</a><br />';
                    }

                echo '</td>';

            } ?>

            </tr>
        </table>

    </div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消