我一直試圖添加一個加載更多按鈕到我的頭版幾個月了。對於我的首頁,我有一個查詢,顯示我的最新帖子,每頁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();