I posted a question a couple of days ago on how to Scroll to Single Post in a custom Wordpress template that I'm developing. What I need in the nut shell is to load a single post into a defined DIV when a particular link is clicked and then scroll down to that DIV holding the newly loaded content. Considering the dynamic content nature of Wordpress or that of any other CMS, the URL of that link cannot be an absolute one.
Unfortunately, there wasn't any concrete answer around at that point of time, so I decided to snoop around a little bit. And since the main issue was loading the content in dynamically, I decide to zoom in on how I can do it with AJAX on Wordpress:
So far, I have gotten a slight idea from a great post (Loading WordPress posts with Ajax and jQuery) by Emanuele Feronato. He basically stores the post ID in the rel attribute of the clickable link and then calls it. Well, there are a few other steps to make this work, but the reason I'm only mentioning the post ID right now is because it seems it's the only part of the equation that isn't right; the post ID loads into the link's rel attribute but fails to load into the .load function.
Just to give you a better idea of what I've gotten in my markup so far:
THE AJAX/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>
SINGLE-HOME.PHP (THIS IS A CUSTOM TEMPLATE)
<?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-->
Just for the record: When the post ID failed to load, I tried installing that particular Kubrick theme used in Emanuele Feronato's demo and made the necessary changes according to his guide but nothing worked.
Does anyone have any idea what's going on or if there is any other way to dynamically load the post ID into the .load function?