我试图让自定义帖子类型的子循环按字母顺序显示,但没有成功。我尝试将ksort和asort应用于$posts
数组,但它只是对ID显示重新排序,而不是对标题进行排序。
// alphabetical menu: https://wordpress.stackexchange.com/questions/119163/displaying-custom-post-type-by-first-letter-through-custom-taxonomy
// get all totems
$args = array(
\'post_type\' => \'totem\',
\'posts_per_page\' => -1
);
$query = new WP_Query($args);
// order by first letter of each totem
$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;
}
wp_reset_postdata();
// order the array
ksort($by_letter);
// fill the array with letters that have no posts
$by_letter = fill_by_letter_array( $by_letter );
?>
<div class="totem-zoo-alphabet-mobile">
<ul>
<?php foreach( $by_letter as $letter => $posts ) {
?>
<li class="<?php echo (empty($posts) ? \'empty\':\'\') ?>">
<?php echo $letter; ?>
<?php if ( ! empty($posts)): ?>
<ul>
<?php
ksort($posts); // ATTEMPTING TO SORT POSTS BY TITLE... FAIL
foreach ( $posts as $post ) {
setup_postdata($post);
echo \'<li><a href="\' . get_permalink() . \'">\'. get_the_title() . \'</a></li>\';
} ?>
</ul>
<?php endif;?>
</li>
<?php
}
wp_reset_postdata();
?>
</ul>
</div>
Functions used from here