简体   繁体   English

加载更多按钮,将加载更多帖子

[英]A load more button that will load more posts

I've been trying to add a load more button to my front page for a few months now. 几个月来,我一直在尝试向首页添加更多加载按钮。 For my front page I have a query that shows my latest posts, 15 per page. 对于我的首页,我有一个查询,该查询显示了我的最新帖子,每页15条。 I want a simple load more button that will start after the first 15 posts and appears after every 15 posts. 我想要一个简单的加载更多按钮,该按钮将在前15个帖子后开始,并在每15个帖子后出现。 I would think this is pretty simple to do, but I just can not figure out for the life of me how to set it up. 我认为这很简单,但是我无法终生弄清楚如何设置它。 If anyone could help I would be extremely appreciative. 如果有人可以帮助我,我将非常感激。

front-page.php front-page.php

 <?php /* * Template Name: */ get_header(); get_template_part ('inc/carousel'); $the_query = new WP_Query( [ 'posts_per_page' => 15, 'paged' => get_query_var('paged', 1) ] ); if ( $the_query->have_posts() ) { ?> <div id="ajax"> <?php $i = 0; $j = 0; while ( $the_query->have_posts() ) { $the_query->the_post(); if ( $i % 5 === 0 ) { // Large post: on the first iteration and every 7th post after... ?> <div class="row"> <article <?php post_class( 'col-sm-12 col-md-12' ); ?>> <div class="large-front-container"> <?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?> </div> <div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div> <h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'front-shop-the-post' ); ?> <?php get_template_part( 'share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> </div> <?php } else { // Small posts ?> <?php if($j % 2 === 0) echo '<div class="row">'; ?> <article <?php post_class( 'col-sm-6 col-md-6' ); ?>> <?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?> <div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div> <h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part( 'front-shop-the-post' ); ?> <?php get_template_part( 'share-buttons' ); ?> <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> <?php $j++; if($j % 2 === 0) echo '</div>'; ?> <?php } $i++; }?> </div> <?php if(get_query_var('paged') < $the_query->max_num_pages) { ?> <?php } } elseif (!get_query_var('paged') || get_query_var('paged') == '1') { echo '<p>Sorry, no posts matched your criteria.</p>'; } get_footer(); 

I'm not going to go into a huge amount of depth but you're going to need a button something like this at the bottom of your post <div id="load-more">Load More< /div> 我不会深入探讨,但是您需要在帖子底部使用类似这样的按钮<div id="load-more">Load More< /div>

You'll then need some javascript / jQuery to watch for when that button is pressed 然后,您需要一些javascript / jQuery来观看何时按下该按钮

$('#load-more').on('click', function() {
   console.debug("Load More button pressed");
});

From there you're going to need to make a little php script to get the next 15 posts. 从那里,您将需要制作一些php脚本来获取接下来的15个帖子。 You will then need to call this from the javascript. 然后,您需要通过javascript调用它。 I don't write WP code very often but it might look a little something like this 我不经常写WP代码,但看起来可能像这样

$lastPostId = $_GET['lastid']; // this will need to be passed from the javascript at a later point.
$posts = [];
$the_query = new WP_Query([ 
    'posts_per_page' => 15, 
    'paged' => get_query_var('paged', $lastPostId) 
]); 

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) { 
        $the_query->the_post(); 
        $posts[] = ""// See comment bellow
    }
}
$response = ["posts" => $posts];
echo json_encode($response);

From here you'll need to figure out how you want to pass your post's data back to the javascript. 在这里,您需要弄清楚如何将帖子的数据传递回javascript。 You have 2 options here. 您在这里有2个选项。 You can render the html view inside of the response or parse the data into a json like format and return that back to your javascript. 您可以在响应中呈现html视图,也可以将数据解析为json之类的格式,然后将其返回给JavaScript。

From here we're going to need to handle the response from your php script. 从这里开始,我们将需要处理来自您的php脚本的响应。

Note: it's up to you to figure out how you're going to handle getting the last id part. 注意:由您自己决定如何处理最后的id部分。 Logically you can just add a data-id to each post container and then use jquery to get the last-child's data-id 从逻辑上讲,您可以仅向每个帖子容器添加一个data-id ,然后使用jquery获取最后一个孩子的data-id

$('#load-more').on('click', function() {
   var lastId = 15;
   // Now we make the query and handle it
   $.get("myabovephpscript.php", {lastid: lastId}, function( data ) {
       // for this example I'm going to to assume you've prebuilt the
       // html so we're going to append it to the page.
       for( let i = 0; i < data.posts.length; i ++ ) {
           $( '#postsContainer' ).append( data.posts[i].postData );
       }
   });
});

and we're done. 我们完成了。 All you have to do is fill in the blanks and tidy it all up. 您所要做的就是填补空白并整理所有内容。 Small disclaimer. 小免责声明。 I've not tested this at all. 我根本没有测试过。 I've just written these types of systems to many times. 我已经多次编写这些类型的系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM