从ajax加载Wordpress特定的帖子模板

时间:2012-11-09 05:54:40

标签: ajax wordpress

我有一个模板portfolio-modal.php,用于为帖子的模态窗口构建html。

我可以从functions.php

加载php模板
function ajaxPortfolioItem(){  
  //get the data from ajax() call  
   $postID = $_GET['postID'];  
   $portfolioItem = get_template_part('portfolio','modal');
   die($portfolioItem);  
}
add_action( 'wp_ajax_ajaxPortfolioItem', 'ajaxPortfolioItem' ); 

但是如何将$postID传递给模板,以便它可以呈现正确的帖子?

这似乎是我看过一百万个主题的事情,但我已经搜遍了所有人,但找不到答案。

P.S。我不是WP人,所以也许我正在接近这个错误。

1 个答案:

答案 0 :(得分:3)

在我正在处理的主题上,我正在使用此

加载点击事件的特定页面
$(".YourOpeningButton").click(function(){
        var post_link = $(this).attr("href");
        $("#YourContainer").html("loading...");
        $("#YourContainer").load(post_link + " #container > * ");
    return false;
    });

然后您只需在WP中创建一个页面,并在主题中添加指向此页面的链接。

<a href="<?php echo get_option('home'); ?>/YourPage/" class="">link</a>

当您点击YourOpeningButton时,应检查href并在YourContainer中打开此页面。我希望它有所帮助...