Wordpress帖子在文章alpahabetic排序

时间:2016-09-16 07:53:26

标签: wordpress dictionary indexing alphabetical

无论如何还是有任何插件用于按字母顺序列出a-z,当你点击任何字母时,它会显示以alpa开头的内容。大多数插件需要逐个添加每个将要编制索引的项目,因为我已经有了很长的列表。实施例:

A | B | C | D | ë

BROS

BROTHER

BEGGIN

Like in this picture but this plugin also wants items add one by one

1 个答案:

答案 0 :(得分:0)

确实不需要插件:https://wordpress.stackexchange.com/questions/67271/display-all-posts-starting-with-given-letter

可能对您有用的解决方案(例子来自上面的链接):

<ul class="posts">
     <?php 
     global $wpdb; 
     $request = "a" // could be any letter you want
     $results = $wpdb->get_results(
            "
            SELECT * FROM $wpdb->posts
            WHERE post_title LIKE '$request%'
            AND post_type = 'post'
            AND post_status = 'publish'; 
            "
     ); 
     if ( $results ) 
     {
        foreach ( $results as $post ) 
        {
            setup_postdata ( $post ); 
            ?> 
            <li>
                ... loop stuff here (the_title, the_permalink) ... 
            </li>
            <?php 
        }
     } 
     else 
     {
        ?> 
        <div class="alert">No clubs found for that letter. Please try again, or use the search at the top.</div>
        <?php
     }
     ?>
</ul>

您可以使用$ _GET []获取您的网址,因此您可以将上面的代码更改为:

<ul class="posts">
<?php 
    global $wpdb;
    $request = 'a';
    if ( isset( $_GET[ "bl" ] ) ) {
        $request = sanitize_text_field( $_GET[ "bl" ] );
    }
     $results = $wpdb->get_results(
            "
            SELECT * FROM $wpdb->posts
            WHERE post_title LIKE '$request%'
            AND post_type = 'post'
            AND post_status = 'publish'; 
            "
     ); 
     if ( $results ) 
     {
        foreach ( $results as $post ) 
        {
            setup_postdata ( $post ); 
            ?> 
            <li>
                //... loop stuff here (the_title, the_permalink) ... 
            </li>
            <?php 
        }
     } 
     else 
     {
        ?> 
        <div class="alert">No content found for that letter.</div>
        <?php
     }
     ?>
</ul>