[英]adding a load more posts button
I want to add a simple load more button to the front page of my wordpress site that will load more posts. 我想在我的wordpress网站的首页上添加一个简单的加载更多按钮,以加载更多帖子。 I'm using a query and bootstrap to have 1 post per line, and then 2, and then 1, and so on. 我正在使用查询和引导程序,每行有1个帖子,然后是2个,然后是1个,依此类推。 I want 15 posts to be displayed and then a load more button, and then when you press the button 15 more posts are loaded. 我希望显示15个帖子,然后显示“加载更多”按钮,然后当您按下该按钮时,又加载了15个帖子。 I've started to try to add this button, but every time I try it does not work. 我已经开始尝试添加此按钮,但是每次尝试均不起作用。 I have tried plugins and making my own. 我尝试了插件,并制作了自己的插件。 If anyone can help me add in a load more button I would be extremely appreciative. 如果有人可以帮助我添加更多加载按钮,我将非常感激。
my 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) { } } elseif (!get_query_var('paged') || get_query_var('paged') == '1') { echo '<p>Sorry, no posts matched your criteria.</p>'; } get_footer();
EDITS FROM DOMINIQUE'S ANSWER 多米尼基答案的编辑
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) { ?> <button id=load-more>load more</button> <?php } } elseif (!get_query_var('paged') || get_query_var('paged') == '1') { echo '<p>Sorry, no posts matched your criteria.</p>'; } get_footer();
loop.php loop.php
<?php if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); <?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++; }?> } ?>
functions.php functions.php
//LOAD MORE add_action( 'wp_ajax_load_more', 'load_more' ); function load_more() { if ( isset($_POST['action']) && $_POST['action'] === 'load_more' ){ $loopFile = $_POST['loop_file']; $paged = $_POST['page_no']; $posts_per_page = $_POST['posts_per_page']; $args = array('paged' => $paged, 'post_type' => 'post', 'posts_per_page' => $posts_per_page); $query = new WP_Query($args); get_template_part( $loopFile ); } wp_reset_query(); exit; }
js js
// AJAX to grab more posts, wrap with a vanilla javascript or // jQuery click event function. function loadMorePosts(pageNumber, postsPerPage) { var query = 'action=load_more&page_no=' + pageNumber + '&loop_file=loop&posts_per_page=' + postsPerPage; jQuery.ajax({ url: '/wp-admin/admin-ajax.php', type: 'post', data: query, success: function(response){ // handle response } }); }
One approach you can take is to move your query to functions.php and add an action to wp_ajax. 您可以采用的一种方法是将查询移至functions.php并向wp_ajax添加一个操作。 For example: 例如:
add_action( 'wp_ajax_load_more', 'load_more' );
function load_more() {
if ( isset($_POST['action']) && $_POST['action'] === 'load_more' ){
$loopFile = $_POST['loop_file'];
$paged = $_POST['page_no'];
$posts_per_page = $_POST['posts_per_page'];
$args = array('paged' => $paged,
'post_type' => 'post',
'posts_per_page' => $posts_per_page);
$query = new WP_Query($args);
get_template_part( $loopFile );
}
wp_reset_query();
exit;
}
Create a loop.php that resides in the root of your theme. 创建一个位于主题根目录中的loop.php。 This will be a template for your posts that can be iterated over: 这将是您的帖子的模板,可以对其进行迭代:
<?php if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// populate your post template here
echo '<h2>' . get_the_title() .'</h2>';
}
?>
Place this code in a javascript file. 将此代码放在javascript文件中。 When you need to grab more posts make an AJAX call to the action 'load_more': 当您需要获取更多帖子时,请对动作“ load_more”进行AJAX调用:
// AJAX to grab more posts, wrap with a vanilla javascript or
// jQuery click event function.
function loadMorePosts(pageNumber, postsPerPage) {
var query = 'action=load_more&page_no=' + pageNumber +
'&loop_file=loop&posts_per_page=' + postsPerPage;
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'post',
data: query,
success: function(response){
// handle response
}
});
}
This is a very simple implantation, you will want to include a NONCE for security and handle when max number of pages has been reached. 这是一个非常简单的植入,您将需要包括一个NONCE来确保安全性,并在达到最大页面数时进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.