我有一个元框,其中有一个文本框,用于获取自定义帖子类型的标题列表。到目前为止,它运行良好,但问题是我找不到一种使用wordpress内置的建议插件实现键/值自动完成的方法。这是我的代码
/**
* Return list of artists for autocomplete. .
*
* @since 1.0.0
*/
public function list_artists() {
// This only send the post titles not the id
foreach($result as $k => $v) {
echo $v . "'n";
}
// This doesn't work and sends the whole text as json string
$results = array(1 => 'Raheel', 2 => 'Zain', 3 => 'Vicky');
echo json_encode($results);
die();
}
/**
* Provide a dashboard view for the plugin
*
* This file is used to markup the public-facing aspects of the plugin.
*
* @link http://example.com
* @since 1.0.0
*
* @package Songs_Cloud
* @subpackage Songs_Cloud/admin/partials
*/
function show_album_meta_box() { ?>
<label for="artist">Artist</label>
<input type="text" id="artist" name="artist">
<script type="text/javascript">
jQuery(function($) {
$("#artist").suggest(ajaxurl + "?action=sc_list_artists", { delay: 500, minchars: 2 });
});
</script>
<?php }
我不愿意使用下拉列表,因为艺术家可能有1000个,在管理部分看起来不太好。
有没有可能使用建议插件或我可以使用的任何其他方法来实现这一点?
我认为我应该使用Select2
这样,我只需要动态绑定下拉列表并加载select2插件,这样它就为管理部分提供了一个很好的界面,而不是长下拉列表。