我一直试图添加一个加载更多按钮到我的头版几个月了。对于我的首页,我有一个查询,显示我的最新帖子,每页15。我想要一个简单的加载更多按钮,它会在前15个帖子后开始,并在每15个帖子后出现。我认为这样做很简单,但我无法弄清楚如何设置它的生活。如果有人能帮助我,我会非常感激。加载更多的按钮,将加载更多的帖子
前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();