【问题标题】:Timber: Search results not working木材:搜索结果不起作用
【发布时间】:2023-03-19 22:58:01
【问题描述】:

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

我的搜索结果页面无法正常工作。 WordPress 似乎忽略了 search.php 文件并使用了 index.php 文件。我得到的搜索结果只是显示网站上与搜索查询无关的每个“帖子”的挑逗。除了标准的 WordPress 'Post' 和 'Page' 内容外,我还有一个 CPT 和 ACF 扩展类别内容需要显示在搜索结果中。

我有一个 search.php 和 search.twig 文件,并且我确定正在使用正确的 TWIG 模板。

我的搜索.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}}/">

感谢收看!

【问题讨论】:

    标签: wordpress timber


    【解决方案1】:

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

    搜索.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>
    

    value="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>
    

    【讨论】:

    • 谢谢罗伯特 - 我会试试这个。实际上,我昨天刚刚想出了一个解决方法,我将要发布。我安装了重定向插件并创建了一个正则表达式重定向。来源:/?search=(.*) 目标:s=$1
    • 你不应该需要任何 .htaccess 重定向,尽管它工作得很好。通过将 name="s" 添加到输入中,WP 将向 URL 添加正确的查询参数,并且它将按预期工作。在我弄清楚之前我撞了很长时间,哈哈
    • 哇!如此容易,却又如此难以捉摸。这成功了——谢谢!虽然在 Timber 中进行搜索非常棒,但如果可以使用对用户更友好的 URL,那就太好了。不过现在,我会接受它。再次感谢!
    • 谢谢@robertguss!很好的解释,而且效果很好! :)
    【解决方案2】:

    在你的 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>
    

    试试看,如果成功了就告诉我。祝你好运

    【讨论】:

    • 嗨 Usama - 感谢您的关注。我尝试将以上内容添加到我的 search.php 模板中,但不幸的是,没有任何改变(我还尝试了所有常见的事情:硬清除缓存;重新保存永久链接)。我认为 WP/Timber 甚至没有查看 search.php 文件。我安装了 What the File 插件,它告诉我 index.php 文件正在用于显示结果。有趣的是,我的搜索结果页面正在使用查询:?search=[searchQuery]。当我手动将其编辑为:/?s=[searchQuery] 它确实使用了 search.php 模板,我只是没有得到任何结果。
    • 我设法让搜索工作,但只能通过在 .htaccess 文件中创建重写规则(我在主题的 functions.php 文件中编写的任何函数都不会影响重写规则 - 不知道为什么将是)。这是 .htaccess 重写:RewriteCond %{QUERY_STRING} search=(.*) RewriteRule ^$ /?s=%1 [R,L]
    • @AlAvery Timber 目前与搜索无关。它是基本的 WordPress 东西,我们只是利用树枝来列出搜索结果。您可以在 search.php 级别进行调试吗?从你的 $context['posts'] 返回什么?
    • 是的,我认为这是一个模板层次结构问题,但令人困惑。理论上,当它存在于主题根目录中时,search.php 应该优先(确实如此)。但是,似乎正在使用 index.php 文件和用于演示的 front-page.twig 文件。我的 search.php 文件的代码在上面,但它被绕过了。有关 URL 查询的某些内容导致使用 index.php。如果我手动将 URL 从 /?search= 更改为 /?s= 它可以工作。
    • @AlAvery,我不知道为什么它不起作用。查看WordPress docs for Search 他们使用 s 作为查询参数。如果它与 s 一起使用,那么您可以使用它而不是 search 尝试从您的搜索查询中转储您的结果并查看它将落在哪里。
    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多