如何在工艺页面cms /树枝中显示搜索结果条目?

时间:2018-08-10 00:12:04

标签: twig octobercms craftcms

这是我的第一个Craft项目。 该项目使用工艺2。 我无法在结果页面上显示卡片/输入结果。 在结果页上,如果我在侧栏中搜索某个人,则会显示“例如,找到16条julian的结果。重置过滤器”,但是在主要部分中没有显示卡,并且其完全空白。 我不确定这是如何工作的,因此,如果有人可以花一点时间来帮助我,并解释一下它的工作方式,为什么我在主要部分看不到结果,我将不胜感激! 预先谢谢

在索引页面上导航

{% set group = group|default('global') %}
{% set scope = scope|default(craft.entries.limit(null)) %}
{% set people = craft.entries.section('People') %}
<nav class="sidenav">
<h3 data-target=".sidenav__content" class="sidenav__heading js-accordion-toggler">
All People
</h3>

<div class="sidenav__content is-hidden is-transparent">
    <ul>
        {% for entry in people %} {% set active = craft.request.path ~ '/' matches "|^#{people.uri}/|" %}
        <li class="sidenav__item {{ active ? 'sidenav__item--active' : '' }}">
            <a class="sidenav__link" href="{{ entry.url }}">{{ entry.title }}</a>
        </li>
        {% endfor %}
    </ul>

    <form class="js-search-form" action="{{ url('search/results') }}">
        <div class="sidenav__search">
            <input class="sidenav__input js-search-form-input" type="search" name="q" placeholder="Search">
            <input type="hidden" name="s" value="{{ group }}">
            <button class="sidenav__submit js-search-form-trigger" type="submit"></button>
        </div>
    </form>
    {% if results is defined and query is defined %}
    <div class="sidenav__search-results">{{ results }} results found for {{ query }}.
        <a href="/{{ group }}">Reset filters</a>
    </div>
    {% endif %}
</div>

结果页面

    {% extends "_layouts/_master" %}

    {% set section = craft.request.getParam('s') %}
    {% set query = craft.request.getParam('q') %}

    {% if section == 'publications' %}
        {% set section_title = 'Publications' %}
        {% set params = {
            section: 'publications',
            with: [
                'publicationCategory',
                'publicationAuthor'
            ]
        } %}

    {% elseif section == 'news' %}
        {% set section_title = 'News & Events' %}
        {% set params = {
            section: 'news',
            with: [
                'newsCategory',
                'newsAuthor'
            ]
        } %}

    {% elseif section == 'People' %}
        {% set section_title = 'People' %}
    {% set params = {
            section: 'People',
            with: [
                'relatedPerson'
            ]
        } %}
    {% endif %}

    {% block meta %}
        {% do craft.sproutSeo.meta({
            default: 'evolution7MetaDefaults',
            title: section_title,
        }) %}
    {% endblock %}

    {% set results = craft.entries(params).search(query).order('score') %}
    {% set category = craft.categories.slug() %}

    {% paginate results.limit(10) as pageInfo, entriesOnPage %}

    {% block content %}
        {% include '_includes/hero-category' with {
            'title': section_title,
            'entry': category,
            'defaultHero' : defaultImages.heroBackground.first,
        } only %}

        {% spaceless %}
        <main class="page page--cols">
            <div class="grid grid--full grid--flex">
                <div class="grid__item lap-and-up-one-quarter">
                    {% include '_includes/sidenav' with { 'group': section, 'var': 'all', 'query': query, 'results': results|length } only %}
                </div>
                <div class="grid__item lap-and-up-three-quarters">
                    <div class="page__content page__content--list">
                        {% if results|length %}
                            <ul>
                                {% for entry in entriesOnPage %}
                                    {% if section == 'publications' %}
                                        {% include 'publications/_card' with { 'entry': entry } only %}
                                    {% elseif section == 'news' %}
                                        {% include 'news/_card' with { 'entry': entry } only %}
                                    {% elseif section == 'people' %}
                                        {% include 'people/_card' with { 'entry': entry } only %}
                                    {% endif %}
                                {% endfor %}
                                {% include '_includes/pagination' %}
                            </ul>
                        {% else %}
                            <p class="page__noresults">Your search for “{{ query }}” didn’t return any results.</p>
                        {% endif %}
                    </div>
                </div>
            </div>
        </main>
        {% endspaceless %}
    {% endblock %}

1 个答案:

答案 0 :(得分:0)

尝试一次使用分页代码搜索:

{% set query = craft.app.request.getParam('search_query') %} 
    {% set all_query = craft.app.request.getParam('all_search') %} 
    {% if query is not empty %} 
        {% set allEntries = craft.entries.search(query).limit(10) %}
        {% paginate allEntries as entries %} 
        {% if entries|length %}
            <div class="col-md-8 offset-2">    
                <div class="searchresultsstatus">
                    <p>Showing <strong>{{ paginate.first }}</strong>–<strong>{{ paginate.last }}</strong> of <strong>{{ paginate.total }}
                    </strong> results for: <strong class="query">{{ query }}</strong></p>
                </div>
            </div>
            {% for entry in entries %}
                    {# Show data in Loop #}
           {% endfor %}
    {% endif %}