wordpress loop with jquery .load()

一世执手 提交于 2020-01-17 05:13:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!