doupeng8494 2017-11-27 20:44
浏览 52

主页上的自定义帖子类型的“加载更多”按钮

I have a custom post type called videos and on my home page I want to display three posts at a time with a "load more" button below that when clicked will load the next 12 posts directly below. My code in my front-page.php is:

<?php
    $args2 = array(
      'post_type'      => 'videos',
      'posts_per_page' => '3',
      'offset' => '1'
    );
 ?>
      <div class="triple site-content">
      <h1>Featured Issues</h1>
  <?php $query = new WP_query ( $args2 );
    if ( $query->have_posts() ) { ?>
      <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <div class="col-sm-4" id="<?php echo( basename(get_permalink()) ); ?>">
      <?php global $post; 
      $gethref = $post->post_name; ?>
          <a href="/<?php echo $gethref ?>"><img 
          src="https://img.youtube.com/vi/<?php the_field('video_id'); ?
          >/hqdefault.jpg" alt="<?php the_title(); ?>" class="stills"/></a>
          <span class="subTitles"><?php the_title(); ?></span>
    </div>

  <?php endwhile; } ?>

I'm not sure how to go about tackling this issue. I have tried a few options I've researched online but nothing is seeming to work.

  • 写回答

1条回答 默认 最新

  • dongxian3418 2017-11-27 21:24
    关注

    This is a variant of an effect called infinite scroll. That name is more usually used to describe when the additional page load is triggered by scrolling to the bottom of the page, but it's actually the same thing being triggered by different events.

    It's not something you can do with PHP - it uses Javascript and requires you to already have a working conventional pagination system, so the first thing to do is make sure that's working as expected, so you can navigate between the pages of search results. The part of the page that contains the paginated items must also be wrapped in a div you can easily access.

    Then, you write some Javascript to do the following:

    • On the trigger event, prevent the default behaviour from occurring and make an AJAX request to fetch the next page
    • When you receive the response, retrieve the HTML inside the div containing the paginated items
    • Append the paginated items to the existing ones and update the pagination to point to the next page

    This means that if for any reason a client cannot execute Javascript the site remains usable, although these days that probably only affects search engines, and not all of them.

    It's not that hard to roll your own implementation for this, but there's a few out there already. The one that seems to be most popular is this one, which also has a Wordpress plugin.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法