Toggle navigation
首页
技术
骑行
羽毛球
资讯
联络我
登录
Craft CMS之Search
2017-07-24
CraftCMS
Craft CMS内建的Search,可以使用包含:简单搜索、逻辑搜索、栏位搜索、完全匹配搜索、区域过滤、通配符搜索功能 参考如下: | 搜索语法 | 搜索结果 | | --- | --- | | salty | containing the word “salty”. | | body:salty | where the ‘body’ field contains “salty”. | | salty dog | containing both “salty” and “dog”. | | body:salty body:dog | where the ‘body’ field contains both “salty” and “dog”. | | salty OR dog | containing either “salty” or “dog” (or both). | | body:salty OR body:dog | where the ‘body’ field contains either “salty” or “dog”. | | salty -dog | containing “salty” but not “dog”. | | body:salty -body:dog | where the ‘body’ field contains “salty” but not “dog”. | | "salty dog" | containing the exact phrase “salty dog”. | | body:"salty dog" | where the ‘body’ field contains the exact phrase “salty dog”. | | salty locale:en_us | containing “salty” in the ‘en_us’ locale. | | sal* | containing a word that begins with “sal”. | | *ty | containing a word that ends with “ty”. | | *alt* | containing a word that contains “alt”. | | body:sal* | where the ‘body’ field contains a word that begins with “sal”. | | body::salty* | where the ‘body’ field begins with “salty”. | | body::"salty dog" | where the ‘body’ field is set to “salty dog” and nothing more. | | body:* | where the ‘body’ field contains _any_ value. | # 搜索结果模板 在teamplates目录下,建立search.html文件,简单的输入如下内容: ```html {% set query = craft.request.getParam('q') %} {# Pass the search query directly into the search param: #} {% set results = craft.entries({ search: query }) %} {% for result in results %} <li><a href = "{{result.slug}}">{{result.title}}</a></li> {% endfor %} ``` 模板先获取参数q,然后将该参数传递给craft的search api搜索并返回结果,最后将结果列表出来。 # 搜索测试 访问页面如下页面即可对craft cms中的内容搜索,并呈现搜索结果 /search?q= 需要注意的一点是,如果搜索多个英文单词,默认是“且”的关系,就是搜索结果必须都包含全部的英文单词,这点和百度,Google不一样,如果要实现他们的效果,可以用“OR”关键字,比如 /search?q=apple OR egg # 限定搜索section 如果需要限定搜索的section,可以利用search api的section参数,比如: ```html {% set results = craft.entries({ section: 'blog', search: query }) %} ``` 如果要搜索多个section,可以用“,”隔开: ```html {% set results = craft.entries({ section: 'blog,news', search: query }) %} ``` 参考: * https://craftcms.com/docs/searching
×
本文为博主原创,如需转载,请注明出处:
http://www.supperxin.com
返回博客列表