当 page/post 名称设置为永久链接时,WORDPRESS 中的 Ajax 无法正常工作
Ajax in WORDPRESS Not Working properly when page/post name is set as permlink
我在 WORDPRESS 中遇到 AJAX 问题。
当我将永久链接设置为默认 AJAX 时,效果很好,但是当我将其更改为 postname ajax 时,响应给了我 404 状态。
这是我的 html 代码和进行 ajax 调用的 javascript 代码。
<script>
$("#dp").datepicker({
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
showOn: 'both',
onClose: function( selectedDate ) {
$( "#dp" ).datepicker( "option",selectedDate );
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
alert(xmlhttp.status);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//alert("hi");
//document.getElementById("pdf").innerHTML=xmlhttp.responseText;
window.location.href = response;
//alert("ddd");
}
}
xmlhttp.open("POST","http://xyz.in/wp-content/themes/xyz/option_post_date.php?postdate="+selectedDate,true);
xmlhttp.send();
}
});
</script>
<form action="" method="post" id="newsdate">
<input type="hidden" id="dp" name="dp"/>
</form>
这里是Ajax文件php代码
<?php
include($_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php");
//require_once(".../wp-blog-header.php"); ?>
<?php
$postDate=$_GET['postdate'];
/*function wpa57065_filter_where( $where = '' ) {
// posts in the last 30 days
$postDate=$_GET['postdate'];
$query="select * from wp_posts where post_type='newspaper'";
$data=mysql_query($query);
$dataset=mysql_fetch_array($data);
$datedata=$dataset['post_date'];
$time=substr($datedata,-8);
$newDate=date('Y-m-d', strtotime($postDate));
$where .= " AND post_date = '" . $newDate . " ".$time ."'";
return $where;
}*/
$args = array( 'post_type' => 'NewsPaper',
'numberposts'=>1,
'meta_query' =>array(
array(
'key'=>'publish_date',
'value' => date('Y-m-d', strtotime($postDate)),
'compare' => '='
)));
$newspapers[0] = new WP_Query( $args );
$flag=$newspapers[0]->posts;
$postid=$flag[0]->ID;
if(isset($_GET['pageid']))
{
$pageNo=$_GET['pageid'];
}
else
{
$pageNo=1;
}
$meta_values = get_post_meta( $postid);
echo do_shortcode('[gview file="'.$meta_values['page_'.$pageNo.'_pdf'][0].'" width=100% height=800px]');
?><?php
wp_reset_postdata();
?>
您是否尝试过使用 ajax wordpress 方式?
对于 ajax url:
"<?php echo admin_url('admin-ajax.php'); ?>"
在您传递的日期旁边,您必须传递要在 php 中调用的函数的名称:
action="get_posts"
在functions.php文件中注册函数:
add_action('wp_ajax_nopriv_get_posts', 'get_posts_callback');
add_action('wp_ajax_get_posts', 'get_posts_callback');
function get_posts_callback(){
$postdate = filter_var($_POST['postdate'], FILTER_SANITIZE_NUMBER_STRING);
//echo your stuff
die('');
}
在这里你可以找到一个很好的例子:http://wptheming.com/2013/07/simple-ajax-example/
我在 WORDPRESS 中遇到 AJAX 问题。 当我将永久链接设置为默认 AJAX 时,效果很好,但是当我将其更改为 postname ajax 时,响应给了我 404 状态。 这是我的 html 代码和进行 ajax 调用的 javascript 代码。
<script>
$("#dp").datepicker({
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
showOn: 'both',
onClose: function( selectedDate ) {
$( "#dp" ).datepicker( "option",selectedDate );
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
alert(xmlhttp.status);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//alert("hi");
//document.getElementById("pdf").innerHTML=xmlhttp.responseText;
window.location.href = response;
//alert("ddd");
}
}
xmlhttp.open("POST","http://xyz.in/wp-content/themes/xyz/option_post_date.php?postdate="+selectedDate,true);
xmlhttp.send();
}
});
</script>
<form action="" method="post" id="newsdate">
<input type="hidden" id="dp" name="dp"/>
</form>
这里是Ajax文件php代码
<?php
include($_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php");
//require_once(".../wp-blog-header.php"); ?>
<?php
$postDate=$_GET['postdate'];
/*function wpa57065_filter_where( $where = '' ) {
// posts in the last 30 days
$postDate=$_GET['postdate'];
$query="select * from wp_posts where post_type='newspaper'";
$data=mysql_query($query);
$dataset=mysql_fetch_array($data);
$datedata=$dataset['post_date'];
$time=substr($datedata,-8);
$newDate=date('Y-m-d', strtotime($postDate));
$where .= " AND post_date = '" . $newDate . " ".$time ."'";
return $where;
}*/
$args = array( 'post_type' => 'NewsPaper',
'numberposts'=>1,
'meta_query' =>array(
array(
'key'=>'publish_date',
'value' => date('Y-m-d', strtotime($postDate)),
'compare' => '='
)));
$newspapers[0] = new WP_Query( $args );
$flag=$newspapers[0]->posts;
$postid=$flag[0]->ID;
if(isset($_GET['pageid']))
{
$pageNo=$_GET['pageid'];
}
else
{
$pageNo=1;
}
$meta_values = get_post_meta( $postid);
echo do_shortcode('[gview file="'.$meta_values['page_'.$pageNo.'_pdf'][0].'" width=100% height=800px]');
?><?php
wp_reset_postdata();
?>
您是否尝试过使用 ajax wordpress 方式?
对于 ajax url:
"<?php echo admin_url('admin-ajax.php'); ?>"
在您传递的日期旁边,您必须传递要在 php 中调用的函数的名称:
action="get_posts"
在functions.php文件中注册函数:
add_action('wp_ajax_nopriv_get_posts', 'get_posts_callback');
add_action('wp_ajax_get_posts', 'get_posts_callback');
function get_posts_callback(){
$postdate = filter_var($_POST['postdate'], FILTER_SANITIZE_NUMBER_STRING);
//echo your stuff
die('');
}
在这里你可以找到一个很好的例子:http://wptheming.com/2013/07/simple-ajax-example/