@Xroad,这是最简单的方法。根据具体情况,事情可能会变得更加复杂,但这是最简单的方法:
$query = get_posts(array(\'post_type\' => \'lexique\',\'posts_per_page\' => -1));
$by_letter = array();
while( $query->have_posts() ) { $query->the_post();
global $post;
$letter = substr($post->post_name, 0, 1);
if ( ! isset($by_letter[$letter]) ) $by_letter[$letter] = array();
$by_letter[$letter][] = $post;
}
$context = Timber::get_context();
$context[\'posts_by_letter\'] = $by_letter;
Timber::render(\'lexique.twig\', $context);
lexique.twig
{% for letter in posts_by_letter %}
{% for post in letter %}
<li><a href="{{ post.link }}">{{ post.title }}</a></li>
{% endfor %}
{% endfor %}