您的代码会说:“如果搜索项不为空,并且该确切的搜索项位于我的搜索替换数组中,请替换该项。”因此,与此相反:
!empty($search_replacements[$request_vars[\'s\']]
(也就是说,如果
[$request_vars[\'s\']
在我的
$search_replacements
阵列)
您需要在每次有人搜索时循环搜索替换数组,并在循环中检查当前搜索词是否是循环中当前关键字的子字符串。如果是这样,则进行替换-但仅替换子字符串,而不是整个字符串。
所以,你需要这样的东西:
<?php
function modify_search_term($request_vars) {
// Global is usually not ideal - include the terms inside your filter.
$search_replacements = array(
\'-\' => \' \',
\'&\' => \'replace2\',
\'var\' => \'foo\'
);
// Loop through all of the Search Replacements
foreach($search_replacements as $key => $replacement) {
// Check for current Key in the Search Term
if(stripos($request_vars[\'s\'], $key)) {
// Replace the Key with the Replacement - but don\'t affect the rest of the Search Term
$request_vars[\'s\'] = str_replace($key, $replacement, $request_vars[\'s\']);
}
}
// Always return
return $request_vars;
}
add_filter(\'request\', \'modify_search_term\');
?>