【发布时间】:2023-03-26 13:55:01
【问题描述】:
我尝试在我的 wordpress 循环中进行 ajax 调用,基本上我想在单击标题后将主页中的帖子内容加载到 div 中。 调用 ajax 似乎工作,但我有加载整个页面的问题,我想只加载具有特定 id 的 div 的内容。
这是我的代码:index.php
<?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; ?>
<div id="single-post-container"></div>
这是single.php
<?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>
这是js
jQuery(document).ready(function(){
jQuery.ajaxSetup({cache:false});
jQuery(".post-link").click(function(){
var post_link = jQuery(this).attr("href");
jQuery("#single-post-container").html("content loading");
jQuery("#single-post-container").load(post_link);
return false;
});
});
我尝试在 post_link 之后添加 id #single-post,如下所示:
jQuery("#single-post-container").load(post_link #single-post);
但是当我运行 gulp 时,我遇到了一个阻止我的错误。 我怎样才能达到这个范围?
有人可以给我帮助或建议吗?
【问题讨论】:
-
什么是 post_link?您的 AJAX 调用调用的实际 url 是什么?如果您只是调用 single.php,首先要做的是调用 header() - 因此您可能会得到整个页面。但我可能完全走错了路。
-
post_link 是我点击的帖子的网址。 (如localhost:8888/blog/title-post)。链接的 attr("href")
-
所以如果你在浏览器的地址栏中输入你会看到整个页面 - 帖子和标题等?
标签: javascript php jquery ajax wordpress