Load WordPress post content into DIV using AJAX

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();?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">

        <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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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();?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"></a>

And this is how it looks like now:

<a class="trick" rel="<?php the_permalink ?>" href="<?php the_permalink();?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"></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!

Solution 2

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)

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply