【问题标题】:Is it possible to add an action for certain keywords in a search bar in wordpress?是否可以在 wordpress 的搜索栏中为某些关键字添加操作?
【发布时间】:2020-09-21 21:08:50
【问题描述】:

当用户在搜索栏中搜索时,我有数百个关键字想要执行特定操作。有没有办法为使用 PHP 的 wordpress 网站做到这一点?如果是这样,我将如何编辑搜索结果 php 文件以不显示结果列表而是执行操作。

例如,用户在 WP 搜索栏中输入“蓝色”并点击“搜索”。它们被带到paint.com/blue,而不是一页搜索结果。

然后对于某些其他关键字,它们会被带到当前网站上的页面。例如,用户在 WP 搜索栏中输入“green”并点击“search”,然后被带到我的 wordpress 上的一个页面www.mywebsite.com/green

我猜这对于每个关键字来说都必须是一个巨大的 IF 语句。

【问题讨论】:

  • if 语句的替代方案可以是数组。关键字作为索引,url 作为值。然后你只需要检查关键字的存在。
  • @RST 这将如何工作?谢谢你的回复

标签: php wordpress


【解决方案1】:

长 if else 序列的替代方案可能如下所示:

<?php

$myWords = [
    "phrase1" => "redirect1",
    "phrase2" => "redirect2",
    "phrase3" => "redirect3",
    "phrase4" => "redirect4",
    ];
    
$location = (array_key_exists(<searchword>, $myWords)) ? $myWords[<searchWord>] : false;

if ($location) {
  header("Location: $location");
  exit;
}
             
<default search continues here>
?>

您可能需要稍微调整一下以满足您的需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2016-04-21
    相关资源
    最近更新 更多