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

Ajax in Wordpress to reload a SELECT every second

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

this 个片段开始,我制作了以下 editing/new 个文件:

  1. 模板-fullwidth.php
  2. functions.php
  3. my.js(新文件)
  4. GetPostDate.php(新文件)

我做了什么

我从一个主题的template-fullwidth.php开始,在post的DIV之间添加了如下代码。我想显示结果的地方在 <div id="MaxPostDate"> 标签之间。

</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 -->

然后我编辑了主题的 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' );


?>

然后,我在主题的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>";

                                       ?>

问题

我认为问题出在您的 AJAX URL 上。

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

 jQuery.ajax({
        url:'<?php echo admin_url('admin-ajax.php'); ?>',
        type:'POST',
        success:function(results) {
            jQuery(".result").html(results);
        }
    });

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

这个my.js在主题的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 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' );