我想通过$month
和$year
单击上一个/下一个链接时通过AJAX获得的值,但我得到的只是-1
内容应该在哪里。我的JavaScript函数和AJAX调用位于自定义页面模板中,page-calendar.php
和functions.php
, see all of the code here. 总之,下面是相关的AJAX脚本:
在里面functions.php
:
/* draw_ajax_calendar($month, $year) function, see link above for that */
// Date variable settings
$month = (int) ($_POST[\'month\'] ? $_POST[\'month\']
: ($_GET[\'month\'] ? $_GET[\'month\']
: date(\'m\')));
$year= (int) ($_POST[\'year\'] ? $_POST[\'year\']
: ($_GET[\'year\'] ? $_GET[\'year\']
: date(\'Y\')));
// Adding the AJAX hooks
add_action(\'wp_ajax_nopriv_draw_ajax_calendar\', \'draw_ajax_calendar\');
add_action(\'wp_ajax_draw_ajax_calendar\', \'draw_ajax_calendar\');
在中编写脚本
page-template.php
(另请参阅calendar-script.js,如下所示):
function nextMonthSwap(nextMonthVal,nextYearVal) {
jQuery("#calendar-container").html("<h1>LOADING</h1>").show();
var url="<?php echo $_ajax; ?>";
jQuery.post(url, {month: nextMonthVal, year: nextYearVal}, function(data){
jQuery("#calendar-container").html(data).show();
});
return false;
}
// attach the link event handlers
jQuery(document).ready(function($) {
$(\'#prev-link\').click(function(){ return prevMonthSwap(<?php echo $prevMonthVal; ?>,<?php echo $prevYearVal; ?>)});
$(\'#next-link\').click(function(){ return nextMonthSwap(<?php echo $nextMonthVal; ?>,<?php echo $nextYearVal; ?>)});
});
最后,HTML也在
page-template.php
:
<div id="calendar-container">
<?php echo draw_ajax_calendar($month,$year); ?>
<a id="prev-link" href="<?php echo $prev_href; ?>">← Previous</a>
<a id="next-link" href="<?php echo $next_href; ?>">Next →</a>
</div>
我引用了
this similar post 并尝试将我的JS从
page-calendar.php
到
calendar-script.js
并将脚本包含在
functions.php
:
function enqueue_calendar_script() {
// embed the javascript file that makes the AJAX request
wp_register_script( \'calendar-script.js\', get_bloginfo(\'stylesheet_directory\').\'/scripts/calendar-script.js\');
wp_enqueue_script( \'calendar-script.js\' );
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( \'calendar-script.js\', \'wp_ajax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
}
add_action(\'wp_head\', \'enqueue_calendar_scripts\');
将
calendar-script.js
像这样又回到了通过
$month
和
$year
通过URL的值。登录和注销的行为相同,根据页面源,正在调用脚本。
如果这是长篇大论,请道歉,尽量彻底。非常感谢您的帮助!