Jquery WP-加载一个post';在';将在列表中选择的标题
我正在wordpress中建立一个站点,在该站点的某个部分有一个容器,其中有一个post项目列表(将其视为菜单/侧栏),还有一个空白空间,我将在其中加载内容。当您单击文章列表中的一个标题时,该标题的内容应加载到空div中。单击标题列表中的另一个标题 基本上看起来是这样的:Jquery WP-加载一个post';在';将在列表中选择的标题,jquery,ajax,wordpress,Jquery,Ajax,Wordpress,我正在wordpress中建立一个站点,在该站点的某个部分有一个容器,其中有一个post项目列表(将其视为菜单/侧栏),还有一个空白空间,我将在其中加载内容。当您单击文章列表中的一个标题时,该标题的内容应加载到空div中。单击标题列表中的另一个标题 基本上看起来是这样的: <div class="container"> <ul class="cats"> <?php $my_query = new WP_Query('showposts=
<div class="container">
<ul class="cats">
<?php
$my_query = new WP_Query('showposts=10&cat=4');
while ($my_query -> have_posts()) : $my_query->the_post(); $category = get_the_category();
?>
<li class="trigger">
<h5>
<? the_title(); ?>
</h5>
</li>
<?php
endwhile; wp_reset_query();
?>
</ul>
<div class="the-content">
<? the_content(); ?>
</div>
</div>
-
所以,我已经为ajax找到了一些东西,但我没有这方面的经验。做这件事最好的方法是什么 在获取帖子的同时,还可以将帖子内容存储在一个隐藏的div中,该div具有特定于post的id。比如,
<?php
$my_query = new WP_Query('showposts=10&cat=4');
while ($my_query -> have_posts()) : $my_query->the_post(); $category = get_the_category();
echo '<div id="post_' . $my_query->the_post()->id . '" style="display:none;">' . $my_query->the_post()->content . '</div>';
?>
希望有帮助尝试过,但那只是将标题加载到div中,并且只执行一次。我知道这应该做什么,但确切的方法不是我想要的。如果你想用ajax做到这一点,你可以使用ajax管理挂钩。你可以在这里查阅。基本逻辑是,单击标题时,将向admin-ajax.php发送一个ajax请求,其中包含指定自定义操作的特定参数。该操作将响应向客户端发布数据
<li class="trigger" onclick="showContent(this)" id="post_id">//This id must be the id of post
<h5>
<? the_title(); ?>
</h5>
</li>
function showContent(obj) {
$(".the-content").empty().html($("#" + $(obj).attr('id')).html());
}