使用 Timber (twig) 和 Wordpress 获取数组中的字母
Get letter in array with Timber (twig) and Wordpress
我想用 Timber(树枝)和 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
)
)
)
如何让每个字母生成我的列表?
{% for letter, posts in query_lexique %}
<h1>{{ letter }}</h1>
{% for post in posts %}
...
{% endfor %}
{% endfor %}
我想用 Timber(树枝)和 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
)
)
)
如何让每个字母生成我的列表?
{% for letter, posts in query_lexique %}
<h1>{{ letter }}</h1>
{% for post in posts %}
...
{% endfor %}
{% endfor %}