幾天前我發佈了一個關於如何在我正在開發的自定義Wordpress模板中使用Scroll to Single Post的問題。我在堅果殼中需要的是在單擊特定鏈接時將單個帖子加載到定義的DIV中,然後向下滾動到保存新加載內容的DIV。考慮到Wordpress或任何其他CMS的動態內容性質,該鏈接的URL不能是絕對的。使用AJAX將Wordpress帖子內容加載到DIV中
不幸的是,當時沒有任何具體的答案,所以我決定稍微探聽一下。由於主要問題是動態加載內容,因此我決定放大如何使用Wordpress上的AJAX實現它:
到目前爲止,我從Emanuele的一篇很棒的文章(Loading WordPress posts with Ajax and jQuery) Feronato。他基本上將帖子ID存儲在可點擊鏈接的rel屬性中,然後調用它。好吧,還有其他一些步驟可以使這項工作成爲可能,但是我現在只提到帖子ID的原因是因爲它似乎是這個等式中唯一不正確的部分;帖子ID加載到鏈接的rel屬性中,但無法加載到.load函數中。
只給你什麼,我已經在我的標記至今得到一個更好的主意:
阿賈克斯/ JQUERY IN的header.php
$(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>
SI NGLE-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加載失敗,我嘗試安裝在埃馬努埃萊Feronato的演示中使用特定庫布裏克的主題並作出必要的改變根據他的指導,但沒有任何工作。
有沒有人有任何想法發生了什麼事情,或者如果有任何其他方式動態加載帖子ID到.load功能?
有一種利用內置WP ajax功能的首選方法 - 您可以在這裏看到:[在WordPress中使用AJAX的5個技巧](http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress /) –
[我寫了一篇關於如何讓AJAX在Wordpress中工作的詳細演練。](http://stackoverflow.com/a/26950030/1922144 ) – davidcondrey