【问题标题】:Wordpress AJAX search not workingWordpress AJAX 搜索不起作用
【发布时间】:2019-02-04 02:13:38
【问题描述】:

我正在尝试使用 AJAX 创建搜索功能,以便加载帖子。它目前不显示结果。

我从另一个帖子中获取了这段代码,并在完成的代码被私下发送时玩弄了它。

任何帮助将不胜感激。

functions.php 文件代码

// the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){

    $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'post' ) );
    if( $the_query->have_posts() ) :
        while( $the_query->have_posts() ): $the_query->the_post(); ?>

            <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>

        <?php endwhile;
        wp_reset_postdata();  
    endif;

    die();
}}

脚本(在functions.php中)

// add the ajax fetch js
add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() {
?>
<script type="text/javascript">
function fetch(){

    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
        success: function(data) {
            jQuery('#datafetch').html( data );
        }
    });

}
</script>

html

<input type="text" name="keyword" id="keyword" onkeyup="fetch()">

<div id="datafetch">Search results will appear here</div>

【问题讨论】:

    标签: javascript php html ajax wordpress


    【解决方案1】:

    您的代码中有一些拼写错误,例如额外的} 和稍后在php 代码中丢失的}。请尝试:

    functions.php

    // the ajax function
    add_action('wp_ajax_data_fetch' , 'data_fetch');
    add_action('wp_ajax_nopriv_data_fetch','data_fetch');
    function data_fetch(){
        $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'post' ) );
        if( $the_query->have_posts() ) :
            while( $the_query->have_posts() ): $the_query->the_post(); ?>
    
                <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>
    
            <?php endwhile;
            wp_reset_postdata();  
        endif;
    
        die();
    }
    
    // add the ajax fetch js
    add_action( 'wp_footer', 'ajax_fetch' );
    function ajax_fetch() {
    ?>
    <script type="text/javascript">
            function fetch_search() {
                jQuery.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'post',
                    data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
                    success: function(data) {
                        jQuery('#datafetch').html( data );
                    }
                });
            }
        </script>
    <?php
    }
    

    html

    <input type="text" name="keyword" id="keyword" onkeyup="fetch_search()">
    <div id="datafetch">Search results will appear here</div>
    

    【讨论】:

    • 带有-1的帖子查询将返回您拥有的所有帖子。也许您想将其限制为前 x 个帖子。我还会考虑在 ajax 调用上添加去抖动或限制来限制调用,请参阅:css-tricks.com/the-difference-between-throttling-and-debouncing
    • 感谢您的建议!但是这些修改都没有解决这个问题。
    【解决方案2】:

    您可能希望将方法名称更新为“POST”,因为 javascript 区分大小写

    function fetch(){
    
        jQuery.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'POST',
            data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
            success: function(data) {
                jQuery('#datafetch').html( data );
            }
        });
    
    }
    

    您可能还想进行需要回调的异步调用,因为上述方法将进行同步调用。检查此答案以获取回调方法 jQuery: Return data after ajax call success

    【讨论】:

    • 感谢您的编辑。它似乎并没有解决它。还有其他想法吗?
    • @AbiSaunders 你添加回调函数了吗?
    • 是的,还是没找到结果
    猜你喜欢
    • 2017-06-12
    • 2014-10-07
    • 1970-01-01
    • 2016-06-19
    • 2014-10-01
    • 1970-01-01
    • 2013-09-18
    • 2017-11-27
    相关资源
    最近更新 更多