我有一個使用無頭 WP 設定的 NextJS 站點。所以我使用 axios 來獲取一些效果很好的搜索結果,但是......它只回傳幾位資訊。
id: 67
subtype: "page"
title: "Test Title"
type: "post"
url: "http://urlhere.com"
我正在使用這個端點: http://headlesswp.local/wp-json/wp/v2/search?search= e.target.value
無論如何要回傳更多資料。特別是搜索結果找到的文本片段。就像谷歌本質上是如何做的一樣。因此,搜索“Lorem Ipsum”將回傳另一個值,例如:
snippet: "...Lorem ipsum dolor sit amet, consectetur adipiscing elit..."
干杯
uj5u.com熱心網友回復:
您可以嘗試將custom-search-result.php以下內容放到 plugins 檔案夾中,并在管理門戶中啟用它。
自定義搜索結果.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 );
} );
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/328443.html
標籤:WordPress的 休息
上一篇:DjangoRESTFramework無法發送多個資料
下一篇:當API關閉時請求優先級
