WordPress网站上的无限滚动

时间:2016-08-17 14:28:23

标签: javascript php jquery wordpress

我有一个wordpress页面显示帖子(这是酒店),目前页面显示所有酒店但是我现在正在尝试编写一些jQuery脚本,以便在用户向下滚动页面时以无限滚动显示酒店

目前我有以下内容,但它确实有效,但我不确定从wordpress获取所需数据的最佳方法,以便jQuery可以正确显示酒店。

<?php get_header(); ?>

<?php
$offset = 2;

$destination_slug = get_query_var('destination');
$term = get_term_by('slug', $destination_slug, 'destination');
$hotels = get_posts(array(
    'post_type' => 'hotels',
    'posts_per_page' => $offset,
    'tax_query' => array(
        array(
            'taxonomy' => 'destination',
            'field' => 'term_id',
            'terms' => $term->term_id
        )
    ))
);

$completeList = get_posts(array(
    'post_type' => 'hotels',
    'tax_query' => array(
        array(
            'taxonomy' => 'destination',
            'field' => 'term_id',
            'terms' => $term->term_id
        )
    ))
);

?>


<div class="pure-g">
    <div class="page-content">
       <?php the_field('hotel_description',$term); ?> 
    </div>
</div>

<div class="hotel-listing">
<?php if ( $hotels ) {
    foreach ( $hotels as $post ) :
        setup_postdata( $post ); ?>

            <div class="row pure-g nopadding member">

                                <div class="pure-u-1 pure-u-md-1-2 block1">

                                    <?php
                                    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>

                                    <div class="grid-item-style" style="background:url('<?php echo $feat_image; ?>');">
                                        <div class="tour-title">
                                        </div>
                                    </div>
                                </div>

                                 <div class="pure-u-1 pure-u-md-1-2 block2">

                                    <div class="inner">
                                        <div class="inner-text">
                                            <div class="hotel-count">
                                                  <?php $connected = new WP_Query( array(
                                                    'connected_type'  => 'hotels_to_tours',
                                                    'connected_items' => $post->ID,
                                                ) );

                                                echo $connected->found_posts; ?> Tours
                                            </div> 

                                            <div class="grid-title">
                                                  <?php the_title(); ?>
                                            </div>
                                            <div class="tour-shortinfo">
                                               <?php the_field('short_info'); ?>
                                            </div>
                                            <a href="<?php the_permalink(); ?>">
                                                <button class="tour">
                                                    Read More
                                                </button>
                                            </a>
                                        </div>
                                    </div>

                                </div>

                            </div>

    <?php
    endforeach; 
    wp_reset_postdata();
}
?>
<script>
    var posts = JSON.parse('<?php echo json_encode($completeList); ?>'); 

    console.log(posts);
    var offset = <?php echo $offset?>;

    $(window).scroll(function() {
        if($(window).scrollTop() >= $('.hotel-listing').offset().top + $('.hotel-listing').outerHeight() - window.innerHeight) {
            console.log("hotel end");
            if (offset < posts.length){
                var html = '<div class="row pure-g nopadding member"> <div class="pure-u-1 pure-u-md-1-2 block1">';
                //html += <div class="grid-item-style" style="background:url('<?php echo $feat_image; ?>');"><div class="tour-title"></div></div>
                html += '</div><div class="pure-u-1 pure-u-md-1-2 block2"><div class="inner"><div class="inner-text"><div class="hotel-count">';
                //html+<?php $connected = new WP_Query( array('connected_type'  => 'hotels_to_tours','connected_items' => $post->ID,) );echo $connected->found_posts; ?> Tours
                html += '</div><div class="grid-title">';
                html += posts[offset].post_title;
                html += '</div><div class="tour-shortinfo">';
                //html +=  <?php the_field('short_info'); ?>
                html += '</div>';
                //html += <a href="<?php the_permalink(); ?>">
                html += '<button class="tour">Read More</button></a></div></div></div></div>';

                $( ".hotel-listing" ).append(html );

                offset++;
            }
        }
    }); 
</script>
</div>

<?php get_footer(); ?>

否则将是我正在尝试做的正确方法?

1 个答案:

答案 0 :(得分:0)

您需要通过ajax调用它 请参阅此link