Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form method="post" class="searchbox outline" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
- <input type="text" name="s" class="s" autocomplete="off" placeholder="<?php _e( 'What are you looking for?', 'gtp_translate' ); ?>" />
- <div class="results">
- <ul>
- <li><a href="#">Result</a></li>
- </ul>
- </div><!--End .results-->
- <input type="submit" name="search" value="<?php _e( 'Search', 'gtp_translate' ); ?>" />
- </form><!--End header-search-->
- // Ajax search box
- $('.searchbox .s').keyup(function() {
- var s = $(this).val();
- if( s.length > 0 ) {
- $.ajax( {
- type: 'post',
- url: gtp.ajaxurl,
- dataType: 'json',
- data: {
- action: 'gtp_ajax_search',
- s: s,
- },
- success: function(data) {
- $('.results').show().append(data);
- }
- });
- } else {
- $('.results').hide()
- }
- });
- // Ajax search
- add_action( 'wp_ajax_gtp_ajax_search', 'gtp_ajax_search' );
- add_action( 'wp_ajax_nopriv_gtp_ajax_search', 'gtp_ajax_search' );
- function gtp_ajax_search() {
- if( isset( $_POST['s'] ) ) {
- $s = $_POST['s'];
- $args = array(
- 's' => $s,
- 'posts_per_page' => 5,
- 'post_type' => 'products',
- );
- $results = new WP_Query( $args );
- echo json_encode( $results );
- }
- die;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement