Php Wordpress:使用AJAX将帖子内容传递到div
我试图在Wordpress的引导模式窗口中打开一篇文章。目标是通过Ajax加载帖子内容,因为模式窗口位于页脚中 这就是我到目前为止所拥有的: 我的模板部分加载在页脚中(modal.php)Php Wordpress:使用AJAX将帖子内容传递到div,php,ajax,wordpress,Php,Ajax,Wordpress,我试图在Wordpress的引导模式窗口中打开一篇文章。目标是通过Ajax加载帖子内容,因为模式窗口位于页脚中 这就是我到目前为止所拥有的: 我的模板部分加载在页脚中(modal.php) 您可以使用RESTAPI for WorpAddress(at),通过这种方式,您可以使用ajax加载post内容,并根据需要将其附加到div中 例如: $.get('http://demo.wp-api.org/wp-json/wp/v2/posts/470',function(data){ dat
您可以使用RESTAPI for WorpAddress(at),通过这种方式,您可以使用ajax加载post内容,并根据需要将其附加到div中
例如:
$.get('http://demo.wp-api.org/wp-json/wp/v2/posts/470',function(data){
data = JSON.parse( data );
$('div.selector').html( data.content );
})
我希望这有帮助首先,您必须在header.php或包含ajax调用的页面中声明ajax url:
<script type="text/javascript" language="javascript">
var ajax_url = "<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php";
</script>
function implement_ajax() {
include(TEMPLATEPATH . '/ajax_return.php');
}
add_action('wp_ajax_my_special_action', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_action', 'implement_ajax');
现在,您必须在想要工作的页面内设置ajax调用
<script type="text/javascript">
jQuery(function(){
jQuery('.modal-link').click(function(){
var mypostid = ''; Will be contained element that allows you to manage dinamical content (such as post_id).
jQuery.post(ajax_url, {action : 'my_special_action', post_id : mypostid}, return_function, 'JSON');
});
});
</script>
在jQuery.post之后,您必须调用“return_函数”,该函数允许您管理响应并正确设置模式:
function return_function(data)
{
jQuery('.modal-body').html(data.return);
}
<script type="text/javascript">
jQuery(function(){
jQuery('.modal-link').click(function(){
var mypostid = ''; Will be contained element that allows you to manage dinamical content (such as post_id).
jQuery.post(ajax_url, {action : 'my_special_action', post_id : mypostid}, return_function, 'JSON');
});
});
</script>
query_posts('page_id=' . $_POST['id']);
if(have_posts()) while(have_post()) : the_post();
$dinamic_post_content = get_the_content();
endwhile;
return json_encode(array('return' => $dinamic_post_content));
exit;
function return_function(data)
{
jQuery('.modal-body').html(data.return);
}