如何用Php (WordPress)显示按字母排序的帖子列表



我想通过PHP在wordpress上创建一个词汇表概述页面,可以通过短代码使用。我希望总是显示首字母,然后是以this开头的各个主题(帖子)。

的例子:

苹果

B

香蕉
  • 黑莓

等等…

要实现这个,我使用以下代码:

// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
array(
'posts_per_page'    => -1,
'post_type'         => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
));



echo '<ul>';
if( $all_posts->have_posts()){

foreach( range( 'A', 'Z' ) as $letter ) {

echo '<div class="group_letter"><div class="letter">' . $letter. '</div>';  
while( $all_posts->have_posts() ){
$all_posts->the_post();
$title = get_the_title(); 
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper( substr( $title, 0, 1 ) );                        



if( $initial == $letter ){

echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';

}

}
$all_posts->rewind_posts();

}
} 
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );

到目前为止,它工作,但现在它显示没有帖子的字母。这是它现在的样子

我试图用if查询做到这一点,但到目前为止,我被卡住了。有人能帮帮我吗?致以最良好的问候和感谢!

使用PHP Sort()函数对数组进行排序,然后遍历结果

<?PHP
$list=['apples','popsicles','Zinger','donkeys','bananas','joe',
'Locusts','gazelles','Angels','Popsicle','Dongle','jump','cocoa'
];
//convert all elements to same case
//sorting will sort by case
$list =array_map('strtolower', $list);
//sort the array
sort($list);
$last_letter=null;
foreach($list as $item){
$current_letter=substr($item,0,1);
if($last_letter!=$current_letter){
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$current_letter?>
</div>
<?php
$last_letter=$current_letter;
}
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$item?>
</div>
<?PHP
}
?>

我相信除了通过while循环运行26次之外,还有更好的解决方案。总之,这就是你要找的东西。

// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type'      => 'glossar',
'orderby'        => 'title',
'order'          => 'ASC',
]
);
echo '<ul>';
if ($all_posts->have_posts()) {
foreach (range('A', 'Z') as $letter) {
$foundPostable = false;
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial === $letter) {
if ($foundPostable === false) {
$foundPostable = true;
echo '<div class="group_letter"><div class="letter">' . $letter. '</div>';
}
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
}
$all_posts->rewind_posts();
}
}
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );

至于改进,像这样的东西可能也有效。

// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type'      => 'glossar',
'orderby'        => 'title',
'order'          => 'ASC',
]
);
echo '<ul>';
$startLetter = '';
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial !== $startLetter) {
$startLetter = $initial
echo '<div class="group_letter"><div class="letter">' . $letter . '</div>';
}
echo '<li><a class="glossary-listing" href="/glossar/'. $name . '">' . $title . '</a></li>';
}
echo '</ul>';
}
add_shortcode('glossary', 'glossary');

最新更新