[英]How to integrate baguetteBox.js in the BST WordPress Starter theme?
[英]How to integrate ajax to Wordpress theme?
我正在尝试在wordpress中使用ajax
我有两个文件,php和js
这是我的PHP代码combo_check-out.php
function iRange($first, $last, $format = 'm/d/Y' ) {
$dates = array();
$current = strtotime($first);
$i=1;
while( $i <= $last ) {
$dates[] = date($format, $current);
$current = strtotime('+1 day', $current);
$i++;
}
$time = date("m/d/Y",$current);
return $time;
}
if($_REQUEST)
{
$id = $_REQUEST['parent_id'];
?>
<select name="check-out" id="check-out-date">
<option value="<?php echo iRange($id, 1, $format = 'm/d/Y' ) ?>">"1 Day (Same Day)"</option>
<option value="<?php echo iRange($id, 2, $format = 'm/d/Y' ) ?>">"2 Days"</option>
<option value="<?php echo iRange($id, 3, $format = 'm/d/Y' ) ?>">"3 Days"</option>
<option value="<?php echo iRange($id, 4, $format = 'm/d/Y' ) ?>">"4 Days"</option>
</select>
<?php}?>
这是我的js代码combo_checkout_iRange.js
$(document).ready(function() {
$('#loader').hide();
$('#check-in-date').change(function(){
$('#check-out-date-wrap').fadeOut();
$('#loader').show();
$.post("combo_check-out.php", {
parent_id: $('#check-in-date').val(),
}, function(response){
setTimeout("finishAjax('check-out-date-wrap', '"+escape(response)+"')", 400);
});
return false;
});
});
//JQuery to hide Loader and return restults
function finishAjax(id, response){
$('#loader').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
function alert_id()
{
if($('#check-out-date').val() == '')
alert('Please select a sub category.');
else
alert($("#check-out-date").val());
return false;
}
他们在WordPress以外的地方工作得很好
如何将它们整合到WordPress主题中
注意:这应该在称为“会议”的帖子类型中起作用,所以这就是我在function.php
写的内容
add_action("wp_enqueue_scripts", function() {
if (is_single()) {
if (get_post_type() == 'meetings')
{
wp_enqueue_script('combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array( 'jquery' ), '1.0' ,true);
}
}
});
第一步
您需要点击的网址是:
<?php echo admin_url( 'admin-ajax.php' ); ?>
现在有几种方法可以将其导入js。 那就是你的“ ajaxurl”
第二步
使用url,您需要通过以下操作:
网址是什么?行动= your_ajax_hit
在jquery中,它将像:
$.ajax({
url : ajaxurl+'?action=your_ajax_hit',
type : 'post'
.....
.....
.....
});
第三步
可以添加到functions.php中
add_action("wp_ajax_your_ajax_hit", "your_function_name");
add_action("wp_ajax_nopriv_your_ajax_hit", "your_function_name");
function your_function_name(){
// your code
wp_die();
}
你可以看到它
wp_ajax_ your_ajax_hit
而已
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.