我几天前在StackOverflow上发布了一个问题,关于如何在我正在开发的自定义Wordpress模板中滚动到单个文章。简而言之,当单击特定链接时,我需要将单个帖子加载到已定义的DIV中,然后滚动到包含新加载内容的DIV。考虑到WordPress或任何其他CMS的动态内容性质,该链接的URL不能是绝对的。
不幸的是,在那个时候还没有任何具体的答案,所以我决定四处寻找一下。由于主要问题是动态加载内容,所以我决定重点研究如何在Wordpress上使用AJAX进行操作:
到目前为止,我从Emanuele Feronato的一篇优秀文章(使用Ajax和jQuery加载WordPress文章)中获得了一些想法。他基本上将帖子ID存储在可点击链接的rel属性中,然后调用它。好吧,还有一些其他步骤需要使其工作,但我现在仅提及文章ID的原因是因为它似乎是等式中唯一不正确的部分;文章ID加载到链接的rel属性中,但未加载到.load函数中。
为了让您对我在标记中得到的更好的想法有所了解: 头部文件中的AJAX / JQUERY
有人知道发生了什么或者是否有其他方法在.load函数中动态加载文章ID吗?
不幸的是,在那个时候还没有任何具体的答案,所以我决定四处寻找一下。由于主要问题是动态加载内容,所以我决定重点研究如何在Wordpress上使用AJAX进行操作:
到目前为止,我从Emanuele Feronato的一篇优秀文章(使用Ajax和jQuery加载WordPress文章)中获得了一些想法。他基本上将帖子ID存储在可点击链接的rel属性中,然后调用它。好吧,还有一些其他步骤需要使其工作,但我现在仅提及文章ID的原因是因为它似乎是等式中唯一不正确的部分;文章ID加载到链接的rel属性中,但未加载到.load函数中。
为了让您对我在标记中得到的更好的想法有所了解: 头部文件中的AJAX / JQUERY
$(document).ready(function(){
$.ajaxSetup({cache:false});
$(".trick").click(function(){
var post_id = $(this).attr("rel");
$("#single-home-container").html("loading...");
$("#single-home-container").load("http://<?php echo $_SERVER[HTTP_HOST]; ?>/single-home/",{id:post_id});
return false;
});
});
INDEX.PHP
<?php get_header();?>
<!--home-->
<div id="home">
<!--home-bg-->
<img class="home-bg" src="<?php bloginfo('template_url');?>/images/home-bg.jpg" />
<!--home-bg-->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a class="trick" rel="<?php the_ID(); ?>" href="<?php the_permalink();?>">
<div class="box element col<?php echo rand(2,4); ?>" id="<?php $category = get_the_category(); echo $category[0]->cat_name; ?>">
<?php the_post_thumbnail(); ?>
<span class="title"><?php the_title(); ?></span>
<span class="ex"><?php the_excerpt(); ?></span>
</div>
</a>
<?php endwhile; endif; ?>
</div>
<!--home-->
<div id="single-home-container"></div>
SINGLE-HOME.PHP(这是一个自定义模板)
<?php
/*
Template Name: single-home
*/
?>
<?php
$post = get_post($_POST['id']);
?>
<!--single-home-->
<div id="single-home post-<?php the_ID(); ?>">
<!--single-home-bg-->
<div class="single-home-bg">
</div>
<!--single-home-bg-->
<?php while (have_posts()) : the_post(); ?>
<!--sh-image-->
<div class="sh-image">
<?php the_post_thumbnail(); ?>
</div>
<!--sh-image-->
<!--sh-post-->
<div class="sh-post">
<!--sh-cat-date-->
<div class="sh-cat-date">
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
- <?php the_time('l, F jS, Y') ?>
</div>
<!--sh-cat-date-->
<!--sh-title-->
<div class="sh-title">
<?php the_title();?>
</div>
<!--sh-title-->
<!--sh-excerpt-->
<div class="sh-excerpt">
<?php the_excerpt();?>
</div>
<!--sh-excerpt-->
<!--sh-content-->
<div class="sh-content">
<?php the_content();?>
</div>
<!--sh-content-->
</div>
<!--sh-post-->
<?php endwhile;?>
<div class="clearfix"></div>
</div>
<!--single-home-->
仅供记录:当文章ID无法加载时,我尝试安装了Emanuele Feronato演示中使用的特定Kubrick主题,并根据他的指南进行了必要的更改,但没有任何作用。有人知道发生了什么或者是否有其他方法在.load函数中动态加载文章ID吗?