Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

php - 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();?>">

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...