问题
I have a tabbed sidebar widget for showing my "popular articles , recent articles" with 2 separate loops.
I wanted to implement some Ajax on it so I created a new file called recent-articles.php and pasted the recent articles loop
<?php $recent = new WP_Query("cat=23,4&showposts=8"); while($recent->have_posts()) : $recent->the_post();?>
<a href="<?php the_permalink(); ?>">
<h2><?php the_title(); ?></h2></a>
<h3> <?php the_category(); ?> </h3>
<?php endwhile; ?>
and in my header.php file I wrote
<script type="text/JavaScript">
$(document).ready(function(){
$("#latestArticles").click(function(){
$("#tab2").load("<?php bloginfo('template_directory'); ?>/recent-articles.php");
});
});
</script>
"latestArticles" is the ID of tab button
"tab2" is the ID of div container to show my loop
whenever I try that , this error appears
Fatal error: Class 'WP_Query' not found in C:\AppServ\www\wordpress\wp-content\themes\mytheme\recent-articles.php on line 1
can any one help ??
回答1:
You're loading the recent-articles.php file, but when it executes, it doesn't have any way of knowing what a WP_Query class is. For your code to work, you would need to include the file that defines WP_Query in your recent-articles.php file.
来源:https://stackoverflow.com/questions/6130364/wordpress-loop-with-jquery-load