2011-05-25 35 views
0

我有一個選項卡式的側邊欄小部件,用於顯示我的「熱門文章,最近的文章」 與2個獨立的循環。wordpress循環與jquery .load()

我想要實現的,所以我創建了一個名爲近期-articles.php新文件中的一些Ajax和粘貼在最近的文章循環

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

,在我的header.php文件我寫

<script type="text/JavaScript"> 
    $(document).ready(function(){ 
    $("#latestArticles").click(function(){ 
     $("#tab2").load("<?php bloginfo('template_directory'); ?>/recent-articles.php"); 
    }); 
    }); 
    </script> 

「latestArticles」 是標籤按鈕的ID

「TAB2」 是的div容器的ID,以顯示我的循環

每當我嘗試,出現此錯誤

Fatal error: Class 'WP_Query' not found in C:\AppServ\www\wordpress\wp-content\themes\mytheme\recent-articles.php on line 1 

任何一個幫助?

回答

0

您正在加載recent-articles.php文件,但是當它執行時,它無法知道WP_Query類是什麼。爲了使代碼正常工作,您需要在您的recent-articles.php文件中包含定義WP_Query的文件。

+0

好的,但那是什麼文件? – Bialy 2011-05-25 21:11:55

+0

該文件是wp-blog-header.php。您還需要關閉主題。請參閱本主題中的熱門答案:http://stackoverflow.com/questions/4641545/i-want-to-run-wp-query-on-a-separate-php-file-for-an-ajax-call – patorjk 2011-05-25 21:56:26

+0

感謝patorjk。偉大的作品:) – Bialy 2011-05-26 05:51:17