在Wordpress中使用Ajax每秒重新加载SELECT

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

this片段开始,我做了以下编辑/新文件:

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

我做了什么

我从主题的template-fullwidth.php开始,并在帖子的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' );


?>

然后,将这个my.js放在主题的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_date ) AS MaxDate FROM wp_posts),而我只被写过一次!
zhuangting 回答:在Wordpress中使用Ajax每秒重新加载SELECT

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

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

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

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

,

主题js文件夹中的my.js

jQuery(document).ready(function refresh_div() {
        jQuery.ajax({
           url:'//MySite/mysite/wp-admin/admin-ajax.php',action:'MyAjaxFunction',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' );
本文链接:https://www.f2er.com/3040052.html

大家都在问