【发布时间】:2011-11-23 11:26:24
【问题描述】:
几天前我发布了一个关于如何在我正在开发的自定义 Wordpress 模板中Scroll to Single Post 的问题。简而言之,我需要在单击特定链接时将单个帖子加载到定义的 DIV 中,然后向下滚动到包含新加载内容的 DIV。考虑到 Wordpress 或任何其他 CMS 的动态内容性质,该链接的 URL 不能是绝对的。
不幸的是,当时没有任何具体的答案,所以我决定稍微打听一下。而且由于主要问题是动态加载内容,我决定放大我如何在 Wordpress 上使用 AJAX 来完成它:
到目前为止,我从 Emanuele Feronato 的一篇很棒的帖子 (Loading WordPress posts with Ajax and jQuery) 中得到了一个小小的想法。他基本上是将post ID存储在可点击链接的rel属性中,然后调用它。好吧,还有其他一些步骤可以完成这项工作,但我现在只提到帖子 ID 的原因是因为它似乎是等式中唯一不正确的部分;帖子 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>
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 主题,并根据他的指南进行了必要的更改,但没有任何效果。
有谁知道发生了什么,或者是否有任何其他方法可以将帖子 ID 动态加载到 .load 函数中?
【问题讨论】:
-
有一种使用内置 WP ajax 功能的首选方法 - 您可以在此处查看:5 tips for using AJAX in WordPress