Я пытаюсь сделать поиск в WordPress с автозаполнением. Дело в том, что я не хочу автозаполнять ввод с заголовком сообщения, но с категориями.
Я объясняю свою проблему:
У меня есть таксономия под названием «produits», в которой есть категории, такие как «diesel, kenzo, Louboutin, …», и у меня есть поле поиска.
Я хочу, чтобы в поле поиска показывались категории: дизель, кензо, …
Я знаю, как получить титул, но я не знаю, как это сделать с таксономиями.
Вот мой код:
Searchform
<form id="searched" action="<?php bloginfo('url'); ?>" method="get"> <input id="search" class="autoEmpty" name="s" type="text" placeholder="Quelle réparation cherchez-vous?" autocomplete="false"><input id="search_submit" value="Rechercher" type="submit"> </form>
скрипт в header.php
<script> $(document).ready(function(){ $("#search").autocomplete({ // myevent_name is the id of the textbox on which we are applying auto complete process source:'<?php echo get_bloginfo("template_url");?>/search_results.php', // sunil_results.php is the file having all the results minLength:1, }); }); </script>
И, наконец, мой поиск-results.php
<?php include('../../../wp-load.php'); $term=$_GET["term"]; $my_args = array( 'post_type' => 'products', // post type name "s" => $term //term we are putting in the textbox ); $json=array(); $custom_query = new WP_Query( $my_args ); if ( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post(); $json[]=array( 'value'=> get_the_title() ); } // while loop ends } echo json_encode($json); ?>
в<?php include('../../../wp-load.php'); $term=$_GET["term"]; $my_args = array( 'post_type' => 'products', // post type name "s" => $term //term we are putting in the textbox ); $json=array(); $custom_query = new WP_Query( $my_args ); if ( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post(); $json[]=array( 'value'=> get_the_title() ); } // while loop ends } echo json_encode($json); ?>
В основном это то, где мне нужно получить категории сообщений в формате json.
Большое спасибо, ребята!
Ладно, я понял!
Это код для моего поиска-results.php
<?php include('../../../wp-load.php'); $term=$_GET["term"]; $json=array(); $terms = get_terms( 'produits' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $term ) { $json[]=array( 'value'=> $term->name ); } } echo json_encode($json); ?>
в<?php include('../../../wp-load.php'); $term=$_GET["term"]; $json=array(); $terms = get_terms( 'produits' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $term ) { $json[]=array( 'value'=> $term->name ); } } echo json_encode($json); ?>
Спасибо milo за вашу помощь! Мне нужно было использовать get_terms!