Load Wordpress post content into DIV using AJAX Load Wordpress post content into DIV using AJAX wordpress wordpress

Load Wordpress post content into DIV using AJAX


Well, by a stroke of luck and some coffee with cigarettes, I managed to resolve the issue:

Here's what I did:

1. Test if post ID is captured in the rel attribute and loaded properly in the post_id variable

I inserted an alert call back on the AJAX / JQUERY snippet to see if the post ID was even loading into the post_id variable right. This is how it looked like:

$(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;    });});

So when I clicked on the link, the call back gave the accurate post ID associated with the post. This kinda isolated the problem right down to the URL defined in the .load() function. It seemed that the post ID was just not sufficient to load the post into defined DIV.

2. Change link's rel attribute from post ID to post permalink

I decided that since the post ID was clearly not working I would instead use the post's permalink tag in the link's rel attribute. This is how it link's rel looked like previously:

<a class="trick" rel="<?php the_ID(); ?>" href="<?php the_permalink();?>"></a>

And this is how it looks like now:

<a class="trick" rel="<?php the_permalink ?>" href="<?php the_permalink();?>"></a>

3. Edit .load() function URL / value

Following on from there, I had to make a change to the AJAX / JQUERY snippet so that it will not use the post ID anymore:

$(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;        });    });

As you can see from the above, I've taken the link's rel attribute value (which is now the post's permalink) and thrown it into the post_link variable. This enables me to simply take that variable and place it into the .load() function, replacing http://<?php echo $_SERVER[HTTP_HOST]; ?>/single-home/",{id:post_id} which obviously didn't work.

VOILA! Problem solved.

Though I have yet to test this, I believe that using the permalink in this instance actually cuts out the need to change the permalink structure in Wordpress as instructed by Emanuele Feronato in his post.

So if anyone has the intent to dynamically load Wordpress posts into a defined DIV, you can probably try this out!


Instead of using:

var post_id = $(this).attr("rel");

you should directly fetch the href. They're both the same.

var post_id = $(this).attr("href");

This helps with 2 things:

  1. Less markup in your HTML
  2. You stay away from using rel as a data attribute, which is not a very wise choice nowadays with HTML5. (read here)