如何使用 Php 显示按字母顺序排序的 post 列表 (WordPress )

How to display an alphabetically sorted list of post withe Php (WordPress )

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

示例:

一个

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' );

到目前为止一切正常,但现在它显示的是没有帖子的信件。 This is how it looks now

我已经尝试使用 if 查询来完成它,但到目前为止,我被卡住了。有人能帮我吗? 致以最诚挚的问候,谢谢!

我相信除了 运行 26 次 while 循环之外还有更好的解决方案。不管怎样,这就是你要找的。

// 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');

使用 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

}
?>