Php Wordpress中的Ajax每秒重新加载一个SELECT

Php Wordpress中的Ajax每秒重新加载一个SELECT,php,jquery,ajax,wordpress,custom-wordpress-pages,Php,Jquery,Ajax,Wordpress,Custom Wordpress Pages,我的目标是获取一个现有WP主题的页面并定制一个全宽页面,每1秒重新加载一个MySQL SELECT查询并显示结果 从代码片段开始,我对以下文件进行了编辑/新建: template-fullwidth.php functions.php my.js(新文件) GetPostDate.php(新文件) 我所做的 我从主题的模板fullwidth.php开始,在帖子的DIV之间添加以下代码。我想显示结果的地方在标记之间 </div><!-- .post --> <!--

我的目标是获取一个现有WP主题的页面并定制一个全宽页面,每1秒重新加载一个MySQL SELECT查询并显示结果

从代码片段开始,我对以下文件进行了编辑/新建:

  • template-fullwidth.php
  • functions.php
  • my.js(新文件)
  • GetPostDate.php(新文件)
  • 我所做的

    我从主题的
    模板fullwidth.php
    开始,在帖子的
    DIV
    之间添加以下代码。我想显示结果的地方在
    标记之间

    </div><!-- .post -->
    <!-- START CUSTOM PAGE CODE -->
    <?php
        if ( is_user_logged_in() ) {
        // --- START IF USER LOGGED IN --- //
            // Get the current user name and id
    ?>
            <div id="MaxPostDate"></div>
            <?php      
    
    
    
        // --- END IF USER LOGGED IN --- //
        } else {
        // --- START IF USER NOT LOGGED IN --- //
        echo "<p>You have to log-in to see this content</p>";
        // --- END IF USER NOT LOGGED IN --- //
        } 
    ?>
    
                        <div class="result"></div>
    <!-- END CUSTOM PAGE CODE -->
                        <?php comments_template( '', true ); ?>
    
                    </div><!-- .posts -->
    
    然后,我在主题的
    js
    文件夹中制作了这个
    my.js

    jQuery(document).ready(function refresh_div() {
            jQuery.ajax({
                url:'/MySite/wp-content/themes/hemingway/GetPostDate.php',
                type:'POST',
                success:function(results) {
                    jQuery(".result").html(results);
                }
            });
        }
    
        t = setInterval(refresh_div,1000););
    
    最后生成另一个文件GetPostDate.php

    <?php
    $TableContent = $wpdb->get_results("
                SELECT MAX(`post_date`) AS MaxDate FROM wp_posts"
        );
        foreach($TableContent as $Content){echo "<h1>" . $Content->MaxDate . "</h1>";
    
                                           ?>
    
    
    
    问题

    • DIV id=“MaxPostDate”中未显示任何内容
    • 我已经写了两次相同的查询(
      SELECT MAX(
      post\u date
      )作为wp\u posts中的MaxDate
      ),我只会写一次
        我认为问题在于您的AJAX URL

        在footer.php中执行AJAX,如下所示:

         jQuery.ajax({
                url:'<?php echo admin_url('admin-ajax.php'); ?>',
                type:'POST',
                success:function(results) {
                    jQuery(".result").html(results);
                }
            });
        
        jQuery.ajax({
        url:“”,
        类型:'POST',
        成功:功能(结果){
        jQuery(“.result”).html(结果);
        }
        });
        

        那么您也不需要GetPostDate.php文件。

        这是主题的js文件夹中的my.js

        jQuery(document).ready(function refresh_div() {
                jQuery.ajax({
                   url:'//MySite/mysite/wp-admin/admin-ajax.php',
                    action:'MyAjaxFunction',
                    type:'POST',
                    dataType:json,
                    success:function(results) {
                        jQuery(".result").html(results.result);
                    }
                });
            }
        
            t = setInterval(refresh_div,1000););
        
        主题的functions.php文件最后添加了以下内容:

        function add_myjavascript(){
          wp_enqueue_script( 'ajax-implementation.js', get_bloginfo('template_directory') . "/js/my.js", array( 'jquery' ) );
        }
        add_action( 'init', 'add_myjavascript' );
        
        function MyAjaxFunction(){
          //get the data from ajax() call
        
            $TableContent = $wpdb->get_results("
                    SELECT MAX(`post_date`) AS MaxDate FROM wp_posts"
            );
            foreach($TableContent as $Content){echo "<h1>" . $Content->MaxDate . "</h1>";}
        
          // Return the String
           die($results);
          }
          // creating Ajax call for WordPress
           add_action( 'wp_ajax_nopriv_MyAjaxFunction', 'MyAjaxFunction' );
           add_action( 'wp_ajax_MyAjaxFunction', 'MyAjaxFunction' );
        
        
        ?>
        
        function MyAjaxFunction(){
          //get the data from ajax() call
            $jsonresult = array();
            $TableContent = $wpdb->get_results("
                    SELECT MAX(`post_date`) AS MaxDate FROM wp_posts"
            );
            foreach($TableContent as $Content){
            $jsonresult['result'] = $Content->MaxDate
        
            }
            echo json_encode($jsonresult);
                die();
        
          }
          // creating Ajax call for WordPress
           add_action( 'wp_ajax_nopriv_MyAjaxFunction', 'MyAjaxFunction' );
           add_action( 'wp_ajax_MyAjaxFunction', 'MyAjaxFunction' );
        

        你做错了:通过将你的
        GET
        请求发送到
        /MySite/wp content/themes/hemingway/GetPostDate.php
        你实际上绕过了整个WordPress框架。然后,您的
        GetPostDate.php
        脚本就无法访问任何WordPress API/对象/函数,因此它不会执行您希望它执行的操作。事实上,您的错误日志可能会因此而充满PHP通知/警告/错误消息。请查看文档以了解。