2013-02-15 57 views
0

使用JQuery UI autocomplete當用戶從遠程源填充文本框表單時,我們可以建議項目。 在遠程數據源,我們有這樣的文件:在wordpress搜索表單中使用jquery ui自動完成

<?php 

sleep(3); 
// no term passed - just exit early with no response 
if (empty($_GET['term'])) exit ; 
$q = strtolower($_GET["term"]); 
// remove slashes if they were magically added 
if (get_magic_quotes_gpc()) $q = stripslashes($q); 

$items = array(
"Great Bittern"=>"Botaurus stellaris", 
"Little Grebe"=>"Tachybaptus ruficollis", 
"Black-necked Grebe"=>"Podiceps nigricollis", 
"Heuglin's Gull"=>"Larus heuglini" 
); 


$result = array(); 
foreach ($items as $key=>$value) { 
if (strpos(strtolower($key), $q) !== false) { 
array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key))); 
} 
if (count($result) > 11) 
break; 
} 

// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions 
echo json_encode($result); 

?> 

,它的O.K.和有用的,現在我想在我的WordPress站點使用這個,因爲遠程文件的輸出是JSON,我想用WordPress Plugin JSON API在遠程源中創建JSON版本的Wordpress內容。在使用這個插件,我們可以在WordPress網站搜索和接收JSON的結果,例如:

http://sushiant.com/?json=1&s=mysearchterm 

但我的問題是如何在遠程源使用?

<?php 
if (empty($_GET['term'])) exit ; 
$q = strtolower($_GET["term"]); 
$s = file_get_contents("http://sushiant.com/?json=1&s=".$q); 
//some code here 
echo $s 
?> 
+0

多文章:http://wordpress.stackexchange.com/q/87003。檢查[如何處理](http://meta.stackexchange.com/q/64068/185667)。 – brasofilo 2013-02-15 08:25:35

回答

1

正如提到hereWordpress JSON API返回在頁面上列出的每個崗位的許多屬性。例如,在上述情況下,我們有一些搜索結果,因此它會爲每個帖子返回一些屬性,如url,title,...。 以JSON格式獲取結果頁面的文件內容(就像您已經完成的那樣),看起來您應該使用decode it into PHP,通過PHP選擇您想要的內容,然後通過json_encode返回。

相關問題