I am trying to reload the fresh posts using jQuery. As far as I know, I can't reload the contents of a div inside the page so I reload a file into that div. (Is that correct?)
The problem is that my loaded file gives me a Fatal error: Call to undefined function wp_head()
How can I implement functions 开发者_如何学Cto a newly created file inside the theme directory to work properly?
This is my jQuery
<script language="JavaScript">
$(function() {
var SANAjax = function(){
$('#reservationdetails').empty().addClass('loading')
.load('wp-content/themes/theme/reloadhomeposts.php', function() {
$(this).removeClass('loading')
});
}
setInterval(SANAjax, 15000 );
});
</script>
<div id="reservationdetails"></div>
And this is what i have in reloadhomeposts.php (I have deleted the content though)
<?php $recent = new WP_Query("cat=3,4,5&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
.
.
.
<?php endwhile; ?>
You should be fine (about the WP functionality you are missing) if you add this to your reloadhomeposts.php file before you call any WP function:
require('../../../wp-blog-header.php');
// ... WP_Query call and loop
You're probably getting that error because you are calling the theme file directly
OR
because you aren't incuding the header and footer of the page.
Easy Solve:
Make sure the page loads as wanted. If you dont want to include the header and all that junk, you can load a fragment with jQuery doing something like this:
.load('wp-content/themes/theme/reloadhomeposts.php #postWrapper', function() {
$(this).removeClass('loading')
});
Your AJAX can call /wp-admin/admin-ajax.php , with an action parameter (and whatever you want), then in a plugin file you can associate your ajax call to :
add_action('wp_ajax_YOUR_ACTION_NAME', 'the_function_to_call');
add_action('wp_ajax_no_priv_YOUR_ACTION_NAME', 'the_function_to_call');
YOUR_ACTION_NAME must be the value of the action parameter. The first one, is a public ajax request, means you can call it even if your not logged in The second one, need that you're logged on.
Then the function the_function_to_call can include whatever, it will works !
function the_function_to_call () {
include (TEMPLATEPATH . '/reloadhomeposts.php');
die(); // prevent string 1 to appear after
}
A little complicated but you are using WP internals process for AJAX.
精彩评论