dongliling6336 2016-08-17 14:28
浏览 122
已采纳

WordPress网站上的无限滚动

I have a wordpress page that displays posts (which are hotels), currently the page just displayed all the hotels however I am now trying to write some jQuery scripting to display the hotels in an infinite scroll as the user scrolls down the page.

Currently I have the below, which does work however I'm not sure of the best way to get the required data from wordpress so that jQuery can display the hotels correctly.

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

Otherwise would would be the correct way to do what I'm trying to do?

  • 写回答

1条回答 默认 最新

  • drhe80011 2016-08-17 14:51
    关注

    You need to call it via ajax refer this link

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?