木材:搜索结果无效

Timber: Search results not working

我正在使用 Timber 为当前位于本地开发环境中的 WordPress 网站设置主题。这是我使用 Timber 的第一个站点。使用起来很有趣,但我肯定还有一些东西要学——特别是因为我更像是一名前端开发人员,PHP 不是我的强项。

我很难让我的搜索结果页面正常工作。看起来 WordPress 正在忽略 search.php 文件并使用 index.php 文件。我得到的搜索结果只是显示了网站上与搜索查询无关的每个 'Post' 的摘要。除了标准的 WordPress 'Post' 和 'Page' 内容,我还有一个 CPT 和 ACF 扩展类别内容需要在搜索结果中显示。

我有 search.php 和 search.twig 文件,我确定使用了正确的 TWIG 模板。

我的search.php:

$templates = array( 'search.twig', 'archive.twig', 'index.twig' );
$context = Timber::get_context();

$context['title'] = 'Search results for '. get_search_query();
$context['posts'] = new Timber\PostQuery();

Timber::render( $templates, $context );

我的index.php:

$context = Timber::get_context();
$context['posts'] = new Timber\PostQuery(); //was: Timber::get_posts();

$templates = array( 'index.twig' );
if ( is_home() ) {
    array_unshift( $templates, 'search.twig', 'front-page.twig' );
}
Timber::render( $templates, $context );

请注意,我在上面的模板中添加了 search.twig,否则搜索结果页面将使用 front-page.twig 模板。

我的search.twig:

{% extends "base.twig" %}

{% block content %}
<h1>Your search results for:</h1>
    {% for post in posts %}
        <article class="tease tease-{{post.post_type}}" id="tease-{{post.ID}}">
                <h2><a href="{{post.link}}">{{post.title}}</a></h2>
                <p>{{post.get_preview}}</p>
                {% if post.get_thumbnail %}
                    <img src="{{post.thumbnail.src}}" />
                {% endif %}
        </article>
    {% endfor %}
{% endblock %}

我的搜索表单代码很简单:

<form role="search" method="get" action="{{site.url}}/">

感谢观看!

在您的 Search.php 中,您需要像这样定义您的搜索查询。

$get_search_query = $context['search_query'] =  get_search_query();

如果您的帖子返回了某些内容,请调试您的 search.php 文件,否则您可以使用

$context['posts'] = Timber::get_posts();

最后,您的搜索表单应该如下所示

<form action="{{ site.link }}" role="search">
   <input name="search" type="search" autocomplete="off">
   <button>Search</button>
</form>

尝试并让我知道这是否有效out.Good运气

我在让搜索正常工作时遇到了类似的问题,这是我的文件让它正常工作的样子。希望对您有所帮助:

Search.php

  $templates = array( 'search.twig', 'archive.twig', 'index.twig' );
  $context = Timber::get_context();

  $context['search_query'] = get_search_query();
  $context['posts'] = new Timber\PostQuery();
  $context['pagination'] = Timber::get_pagination();

  Timber::render( $templates, $context );

.twig 文件

<form method="get" role="search" action="{{ site.url }}">
  <input type="text" placeholder="Enter Keywords..." name="s">
  <input type="submit" value="Search" class="submit button--green">
  <input type="hidden" name="post_type" value="post">
</form>

值为="post" 的隐藏输入将限制搜索范围为仅搜索'post' post 类型。您可以添加 CPT 的名称,它也只会搜索该特定的 CPT。在这个 html 中最重要的是

name="s"

这是 WP 用于执行搜索的查询参数。

这是我的 search-results.twig 模板,供您参考以备不时之需:

<section class="section__news-search-results">
  {% for post in posts %}
    <div class="news-item__container">
      <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-4">
          <div class="image">
            {% if post.thumbnail.src %}
              <img src="{{post.thumbnail.src | resize(150, 150) }}" />
            {% endif %}
          </div> {# /.image #}
        </div> {# /.col #}

        <div class="col-xs-12 col-sm-12 col-md-8">
          <div class="copy">
            <p><small>{{ post.date }}</small></p>
            <p><strong>{{post.title}}</strong></p>
            <p>{{post.preview.length(25)}}</p>
          </div> {# /.copy #}
        </div> {# /.col #}
      </div> {# /.row #}
    </div> {# /.news-item__container #}
  {% endfor %}
</section>