2013-02-15 110 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返回。