2

我有一个包含大约 200 个子页面的父页面,它们构成了一个词典。在父页面的模板中,我想查询有允许以下列表。我想显示以该字母开头的每组单词的第一个字母。如果列表中没有显示某个字母,则不应显示该字母。

一个

阿尔伯特

艾伦

阿曼达

账单

鲍勃

布鲁诺

C

查尔斯

克里斯汀

等等....

4

1 回答 1

0

我的回答对你来说来晚了,但也许有人会觉得它有用。我需要同样的东西,所以我不得不写这个模板:

<?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(); ?>
于 2012-04-02T20:06:09.657 回答