最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Adding ELSE argument to AJAX Live Search for "No results found"

programmeradmin1浏览0评论

Having finally being able to make an AJAX driven live search functional, I need help understanding how to add an else argument function so that when there are no posts matching the search query, the search box will display "No results found" or whatever text I choose.

This is the code I have whereof the else argument before the endif crashes the site.

add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'data_fetch');
function data_fetch() {

    $post_search_query = new WP_Query(array('posts_per_page' => -1, 's' => esc_attr($_POST['search_keyword']), 'post_type' => 'post'));

        if ($post_search_query->have_posts()) :

            while ($post_search_query->have_posts()): $post_search_query->the_post(); ?>

        <h5><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h5>
        <span class="live-search-post-excerpt"><?php the_excerpt(); ?></span>

    <?php endwhile;

        wp_reset_postdata();

            else {
            
                echo 'No results found';
        }
    endif;

die();

}

Having finally being able to make an AJAX driven live search functional, I need help understanding how to add an else argument function so that when there are no posts matching the search query, the search box will display "No results found" or whatever text I choose.

This is the code I have whereof the else argument before the endif crashes the site.

add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch', 'data_fetch');
function data_fetch() {

    $post_search_query = new WP_Query(array('posts_per_page' => -1, 's' => esc_attr($_POST['search_keyword']), 'post_type' => 'post'));

        if ($post_search_query->have_posts()) :

            while ($post_search_query->have_posts()): $post_search_query->the_post(); ?>

        <h5><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h5>
        <span class="live-search-post-excerpt"><?php the_excerpt(); ?></span>

    <?php endwhile;

        wp_reset_postdata();

            else {
            
                echo 'No results found';
        }
    endif;

die();

}
Share Improve this question asked Jan 12, 2021 at 7:03 user199578user199578
Add a comment  | 

1 Answer 1

Reset to default 0

Try to not using endif and endwhile, just use curly brakets {} to determinate ifs body or add :after elseelse:`

Your problem is that you mix two syntaxes.

Read this to fully understand how to write alternative syntax https://www.php/manual/en/control-structures.alternative-syntax.php

    add_action('wp_ajax_data_fetch' , 'data_fetch');
    add_action('wp_ajax_nopriv_data_fetch', 'data_fetch');
    function data_fetch() {
    
        $post_search_query = new WP_Query(array('posts_per_page' => -1, 's' => esc_attr($_POST['search_keyword']), 'post_type' => 'post'));
    
            if ($post_search_query->have_posts()){
    
                while ($post_search_query->have_posts()){  $post_search_query->the_post(); ?>
    
            <h5><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h5>
            <span class="live-search-post-excerpt"><?php the_excerpt(); ?></span>
    
        <?php } //end while
wp_reset_postdata();
} //end if
    
         
    
                else {
                
                    echo 'No results found';
            } // end else
        
    
    die();
    
    }
发布评论

评论列表(0)

  1. 暂无评论