如何将 a-z 过滤器添加到 wordpress 搜索结果中? [英] How to add a-z filter to wordpress search result?

查看:42
本文介绍了如何将 a-z 过滤器添加到 wordpress 搜索结果中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我需要按字母顺序过滤我的搜索结果在搜索结果页面的头部会有字母列表A - B - C - .... - Z - 全部

I need to filter my search result alphabetically for example at the head of the search result page there will be list of letters A - B - C - .... - Z - ALL

当我点击任何字母时,结果会显示仅以该字母开头的帖子

and when I click on any letter the result show me the posts that only start with that letter

推荐答案

在 search.php 中关于您的主题

in the search.php on your theme

在循环之前添加

    <?php 

add_action( 'posts_where', 'startswithaction' );
function startswithaction( $sql ){
    global $wpdb;
    $startswith = get_query_var( 'startswith' );

    if( $startswith ){
        $sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $startswith.'%' );
    }

    return $sql;
}
add_action( 'posts_where', 'startswithnumberaction' );
function startswithnumberaction( $sql ){
    global $wpdb;
    $startswithnumber = get_query_var( 'startswithnumber' );

    if( $startswithnumber ){ 
        $sql .= $wpdb->prepare( " AND $wpdb->posts.post_title NOT REGEXP %s ", '^[[:alpha:]]' );
    }

    return $sql;
}

query_posts( $query_string .'&startswith='.$_GET['letter'].'&posts_per_page=-1&startswithnumber='.$_GET['number']);
?>

</code>

然后添加您的链接

echo "<a href='$PHP_Self/?$query_string&number=true' ># </a> - ";


    foreach (range('A', 'Z') as $i)
    {
     $letter =strtolower($i);
        echo "<a href='$PHP_Self/?$query_string&letter=$letter' >$i </a> - ";
    }
    echo "<a href='$PHP_Self/?$query_string' >All </a>

就像当你点击任何一个字母时,搜索结果将被过滤到只以这个字母开头的帖子

like that when u click on any letter the search result will be filtered to the posts that only starts with this letter

这篇关于如何将 a-z 过滤器添加到 wordpress 搜索结果中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆