我几天前发布了一个关于如何在我正在开发的自定义wordpress模板中使用Scroll to Single Post的问题.我在坚果壳中需要的是在单击特定链接时将单个帖子加载到定义的DIV中,然后向下滚动到保存新加载内容的DIV.考虑到wordpress或任何其他CMS的动态内容特性,该链接的URL不能是绝对的.
不幸的是,在那个时间点没有任何具体的答案,所以我决定窥探一下.由于主要问题是动态加载内容,我决定放大我在wordpress上使用AJAX的方法:
到目前为止,我从Emanuele Feronato的一篇伟大帖子(Loading WordPress posts with Ajax and jQuery)中得到了一个小小的想法.他基本上将帖子ID存储在可点击链接的rel属性中,然后调用它.好吧,还有一些其他步骤可以使这项工作,但我现在只提到帖子ID的原因是因为它似乎是方程式中唯一不正确的部分; post ID加载到链接的rel属性中,但无法加载到.load函数中.
只是为了让你更好地了解我到目前为止在我的标记中得到了什么:
HEADER.PHP中的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>
<?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主题,并根据他的指南做了必要的更改,但没有任何效果.
有没有人知道发生了什么或者是否有其他方法可以将帖子ID动态加载到.load函数中?
解决方法:
好吧,幸运的是,一些咖啡加香烟,我设法解决了这个问题:
这是我做的:
1.测试是否在rel属性中捕获了帖子ID并在post_id变量中正确加载
我在AJAX / JQUERY片段上插入了一个警报回调,以查看帖子ID是否甚至加载到post_id变量中.这是它的样子:
$(document).ready(function(){
$.ajaxSetup({cache:false});
$(".trick").click(function(){
var post_id = $(this).attr("rel");
alert(post_id);
$("#single-home-container").html("loading...");
$("#single-home-container").load("http://<?PHP echo $_SERVER[HTTP_HOST]; ?>/single-home/",{id:post_id});
return false;
});
});
因此,当我点击链接时,回拨给出了与帖子相关联的准确帖子ID.这有点将问题隔离到.load()函数中定义的URL.似乎帖子ID不足以将帖子加载到定义的DIV中.
我决定,由于帖子ID显然不起作用,我会在链接的rel属性中使用post的永久链接标记.这就是它的rel之前的样子:
<a class="trick" rel="<?PHP the_ID(); ?>" href="<?PHP the_permalink();?>"></a>
这就是现在的样子:
<a class="trick" rel="<?PHP the_permalink ?>" href="<?PHP the_permalink();?>"></a>
3.编辑.load()函数URL /值
继续之后,我不得不对AJAX / JQUERY片段进行更改,以便它不再使用帖子ID:
$(document).ready(function(){
$.ajaxSetup({cache:false});
$(".trick").click(function(){
var post_link = $(this).attr("rel");
$("#single-home-container").html("loading...");
$("#single-home-container").load(post_link);
return false;
});
});
从上面可以看出,我已经获取了链接的rel属性值(现在是post的永久链接),并将其抛入post_link变量.这使我能够简单地将该变量放入.load()函数中,替换为http://<?PHP echo $_SERVER [HTTP_HOST]; ?> / single-home /“,{id:post_id}显然无效.
瞧!问题解决了.
虽然我还没有对此进行测试,但我相信在这种情况下使用固定链接实际上不需要按照Emanuele Feronato在其post中的指示更改wordpress中的永久链接结构.
因此,如果有人有意图将wordpress帖子动态加载到已定义的DIV中,您可以试试这个!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。