当页面/帖子名称设置为permlink时,WORDPRESS中的Ajax不能正常工作

时间:2015-01-30 09:06:05

标签: ajax wordpress

我在WORDPRESS中遇到AJAX问题。 当我设置permlinks作为默认AJAX工作完美,但当我将其更改为postname ajax responce给我404状态。 这是我的html代码和javascript代码,它使ajax调用。

<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();						
?>

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用ajax wordpress方式?

表示ajax网址:

"<?php echo admin_url('admin-ajax.php'); ?>" 
你传递的postdate旁边的

你必须传递你要在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/