0

我想用 Timber (twig) 和 Wordpress 创建一个字典。

我按字母获取数组,但我需要生成一个字母列表:A - B - C - D ...

$query = new WP_Query(array('post_type' => 'lexique', 'posts_per_page' => -1));

$by_letter = array();
    while( $query->have_posts() ) { $query->the_post();
    $letter = substr($post->post_name, 0, 1);
    if (! isset($by_letter[$letter]) ) $by_letter[$letter] = array();
    $by_letter[$letter][] = $post;
}

if (!empty($by_letter)) {
    ksort($by_letter);
}

$context['query_lexique'] = $by_letter;

所以我需要在这个中取第一个字母 [a] [b] :

Array
(
    [a] => Array
        (
            [0] => WP_Post Object
                (
                    [ID] => 239
                    [post_author] => 1
                    [post_date] => 2016-12-29 14:22:03
                    [post_date_gmt] => 2016-12-29 13:22:03
                )

            [1] => WP_Post Object
                (
                    [ID] => 238
                    [post_author] => 1
                    [post_date] => 2016-12-29 14:21:34
                    [post_date_gmt] => 2016-12-29 13:21:34
                )

        )

    [b] => Array
        (
            [0] => WP_Post Object
                (
                    [ID] => 241
                    [post_author] => 1
                    [post_date] => 2016-12-29 16:34:28
                    [post_date_gmt] => 2016-12-29 15:34:28
                )

            [1] => WP_Post Object
                (
                    [ID] => 240
                    [post_author] => 1
                    [post_date] => 2016-12-29 16:34:07
                    [post_date_gmt] => 2016-12-29 15:34:07
                )

        )

)

请问如何让每个字母生成我的列表?

4

1 回答 1

2
{% for letter, posts in query_lexique %}
    <h1>{{ letter }}</h1>
    {% for post in posts %}
        ...
    {% endfor %}
{% endfor %}
于 2016-12-30T11:19:50.180 回答