我使用下面的代码通过AJAX异步加载同一索引页上的帖子。我也希望能够更新URL,但不确定如何将其添加到我当前拥有的内容中。
最终目标的效果类似于this. 如果你点击一篇文章,你将能够看到上面隐藏的div中加载的文章,并且你会看到地址栏中的URL也会更新。
谁能帮帮我吗?
Index Template
<div id="single-post-container"></div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; endif; ?>
Single Post Template
<?php
$post = get_post($_POST[\'id\']);
?>
<div id="single-post post-<?php the_ID(); ?>">
<?php while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile;?>
</div>
jQuery
$(document).ready(function(){
$.ajaxSetup({cache:false});
$(".post-link").click(function(){
var post_link = $(this).attr("href");
$("#single-post-container").html("content loading");
$("#single-post-container").load(post_link);
return false;
});
});
编辑:另外,我从一些资料中了解到,使用WordPress“内置”admin-ajax.php
已推荐。使用admin-ajax.php
如果是这样的话,我将如何用我目前拥有的东西“把它换掉”?更新(2015年1月4日):
Index Template
<div id="project-container"></div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; endif; ?>
Single Post Template
<div id="single-post post-<?php the_ID(); ?>">
<?php while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile;?>
</div>
一$(document).ready(function(){ $.ajaxSetup({cache:false}); $(".post-link").click(function(){ var post_link = $(this).attr("href"); $("#single-post-container").html("content loading"); $("#single-post-container").load(post_link); return false; }); });
jQuery (includes.js)
// Load posts via AJAX (function($, D){ $.ajaxSetup({cache:false}); $(".post-link").click(function(){ var postID = $(this).attr(\'rel\'); var $container = $("#project-container"); $container.html("content loading"); $.get(D.ajaxurl, {action: \'load-single-post\', pID: postID}, function(content) { $container.html(content); }); }); })(jQuery, site);