您可以尝试将custom-search-result.php
以下内容放在插件文件夹中,并在管理门户中启用它。
自定义搜索结果.php
<?php
/**
* Plugin Name: Custom search result
* Description: Custom search result
* Author: Emptyhua
* Version: 0.1
*/
function my_rest_filter_response($response, $server, $request) {
if ($request->get_route() !== '/wp/v2/search') return $response;
if (is_array($response->data)) {
foreach ($response->data as &$post) {
if (!is_array($post)) continue;
if ($post['type'] !== 'post') continue;
$full_post = get_post($post['id'], ARRAY_A);
if ($full_post['post_content']) {
$content = preg_replace('/\n\s+/', "\n", rtrim(html_entity_decode(strip_tags($full_post['post_content']))));
$post['content'] = $content;
}
}
unset($post);
}
return $response;
}
add_action( 'rest_api_init', function () {
add_filter( 'rest_post_dispatch', 'my_rest_filter_response', 10, 3 );
} );