admin管理员组文章数量:1026989
I am currently making a new site and i am stuck with the autocomplete for the standard search form from wordpress. Anyone knows how can i load my wordpress categories to use in this script? Its a mix with javascript and php(to load my categories from wp)
I am currently making a new site and i am stuck with the autocomplete for the standard search form from wordpress. Anyone knows how can i load my wordpress categories to use in this script? Its a mix with javascript and php(to load my categories from wp)
Share Improve this question asked May 6, 2019 at 13:15 DennisDennis 216 bronze badges1 Answer
Reset to default 0It is not clear what autocomplete function you are using in JS, if it is from a library or custom, but I'm assuming that it expects an array where locations
is passed and not a string, which is what you're feeding it.
The function you should be using is get_terms()
, not wp_list_categories()
, the former being used for returning an array of terms in a taxonomy, the latter is for returning a string of HTML markup. You can use the fields
argument to filter which fields you want included in the results.
Example:
$locations = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'fields' => 'names'
) );
Secondly, as mentioned above, your locations
variable is populated with a string in JS not an array. This might be fine if the autocomplete function were to parse it as JSON automatically, but in that case your string would not be valid JSON anyway as you are outputting a <ul>
markup string inside the brackets. Try this:
var locations = ["<?php echo implode('", "', $locations); ?>"];
I am currently making a new site and i am stuck with the autocomplete for the standard search form from wordpress. Anyone knows how can i load my wordpress categories to use in this script? Its a mix with javascript and php(to load my categories from wp)
I am currently making a new site and i am stuck with the autocomplete for the standard search form from wordpress. Anyone knows how can i load my wordpress categories to use in this script? Its a mix with javascript and php(to load my categories from wp)
Share Improve this question asked May 6, 2019 at 13:15 DennisDennis 216 bronze badges1 Answer
Reset to default 0It is not clear what autocomplete function you are using in JS, if it is from a library or custom, but I'm assuming that it expects an array where locations
is passed and not a string, which is what you're feeding it.
The function you should be using is get_terms()
, not wp_list_categories()
, the former being used for returning an array of terms in a taxonomy, the latter is for returning a string of HTML markup. You can use the fields
argument to filter which fields you want included in the results.
Example:
$locations = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'fields' => 'names'
) );
Secondly, as mentioned above, your locations
variable is populated with a string in JS not an array. This might be fine if the autocomplete function were to parse it as JSON automatically, but in that case your string would not be valid JSON anyway as you are outputting a <ul>
markup string inside the brackets. Try this:
var locations = ["<?php echo implode('", "', $locations); ?>"];
本文标签: searchHow to load WP categories into array for autocomplete
版权声明:本文标题:search - How to load WP categories into array for autocomplete? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745519748a2154260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论