使用jquery将wordpress链接加载到div中
我试图让我的 WordPress 链接加载到 div 中而不是刷新页面。已经在网上尝试了一些教程,但它们似乎不起作用(或不完全),
我自己的试用每次只拉出我的最后一篇文章(它确实加载到 div 中,但它不会生成其他帖子,然后是最后一篇,所以所有我点击链接转到同一篇文章,
有人知道为什么这不起作用吗
?
<?php while (have_posts()) : the_post(); ?>
<div class="WZ-OVER-wereldTitle">
<a href="<?php the_permalink(); ?>" rel="<?php the_ID(); ?>">
<?php the_title(); ?>
</a>
</div>
<script>
$(document).ready( function() {
var postID = $('.WZ-OVER-wereldTitle a').attr('rel');
$('.WZ-OVER-wereldTitle a').click( function() {
event.preventDefault();
$('#content').html('loading...');
$('#content').load('http://www.weetzelf.nl/?p=' + postID);
return false;
});
});
</script>
<?php endwhile; ?>
im trying to make my wordpress links load into a div instead of refreshing a page. have tried some tutorials out there on the net but they dont seem to work (or not completely)
my own tryout only pulls out my last post each time (it does get loaded into the div but it doesnt genereate other posts then the last so all links i click goto the same post.
anyone got an idea why this doesnt work ?
many thanks!
<?php while (have_posts()) : the_post(); ?>
<div class="WZ-OVER-wereldTitle">
<a href="<?php the_permalink(); ?>" rel="<?php the_ID(); ?>">
<?php the_title(); ?>
</a>
</div>
<script>
$(document).ready( function() {
var postID = $('.WZ-OVER-wereldTitle a').attr('rel');
$('.WZ-OVER-wereldTitle a').click( function() {
event.preventDefault();
$('#content').html('loading...');
$('#content').load('http://www.weetzelf.nl/?p=' + postID);
return false;
});
});
</script>
<?php endwhile; ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试使用
.load
加载外部(跨域)请求。这是行不通的。替代方案:使用 James Padolsey 插件 然后调用它。这仅适用于 jquery1.4.4
演示: http://jsfiddle.net/YErfj/
You are trying to load external ( cross domain ) request using
.load
. It won't work.Alternative: Use James Padolsey plugin and then call that. This works till jquery1.4.4 only
Demo: http://jsfiddle.net/YErfj/
你做错的主要事情是在这里:
它的作用是将你的内容加载到你的
#content
div 中。但是 jQuery 的 load() 方法将其目标的整个内容设置为从返回的内容加载()。这就是为什么您只获得最后一篇文章的原因 - 您实际上成功加载了所有文章,一个接着一个,每个新的文章都会替换您上次加载的内容。
尝试类似
That 将创建一个全新的
div
元素,将内容加载到 that 中,并将其附加到您的 #content,而不是替换已有的内容。然而,您正在做的另一件奇怪的事情是多次输出 jQuery 部分,因为它位于 php
while
循环内。因此,我认为您最终会得到与帖子一样多的该脚本的副本,所有副本都在页面加载时运行(一个接一个)。在浏览器中检查站点的源代码,看看是否存在此问题是这样的——我猜你会看到块的多个副本。
要解决这个问题,请将脚本部分移到 php
while
循环之外。您只需要一份副本。The main thing that you're doing wrong is here:
What this does is load your content into your
#content
div. But jQuery's load() method sets the entire content of its target to be whatever was returned fromload()
. That's why you're only getting the last post -- you're actually loading all the posts successfully, one after the other, with each new one replacing what you loaded the last time through.Try something like
That will create a brand new
div
element, load the content into that, and append it to your #content, rather than replacing what's already there.However, the other odd thing you're doing is outputting the jQuery part multiple times, because it's inside the php
while
loop. Because of this, I think you're going to end up with as many copies of that script as there are posts, all running on page load (one after the other.) Check the source of your site in your browser to see whether this is the case -- I'm guessing you'll see multiple copies of your<script>
block.To solve that, move the script part outside of the php
while
loop. You only need one copy of it.