Помогите добавить кнопку "Загрузить больше" ajax
Я пытаюсь добавить плагин "Легко загружать больше" в свою тему (https://wordpress.org/plugins/easy-load-more /). Вы должны завернуть его в <div id="ajax">
, а затем поместить <?php load_more_button(); ?>
туда, куда вы хотите, чтобы кнопка "Загрузить больше" переместилась. Я не могу точно определить правильные места для добавления этого кода (как вы можете видеть по моему коду ниже, в настоящее время он находится в неправильных местах). Ниже приведено изображение того, как я хочу, чтобы кнопка "Загрузить больше" выглядела.
Вот мой текущий front-page.php
<?php
/*
* Template Name:
*/
get_header();
get_template_part ('inc/carousel');
$the_query = new WP_Query( [
'posts_per_page' => 14,
'paged' => $paged
] );
if ( $the_query->have_posts() ) {
load_more_button();
?>
<div id="ajax">
<?php
$i = 0;
while ( $the_query->have_posts() ) { $the_query->the_post();
if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
<article <?php post_class( 'col-md-12' ); ?>>
<?php the_post_thumbnail('large-thumbnail'); ?>
<h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
<?php get_template_part( 'share-buttons' ); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
</article><?php
} else { // Small posts ?>
<article <?php post_class( 'col-md-4' ); ?>>
<?php the_post_thumbnail( 'medium-thumbnail' ); ?>
<h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
<?php get_template_part( 'share-buttons' ); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
</article>
<?php
}
$i++;
}
wp_reset_postdata();?>
</div>
<?php
}
else {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
load_more_button();
get_footer();
1 answers
Ваша оболочка div находится в вашем цикле while и разделяется вашим предложением if. Это не может работать должным образом, так как ваш цикл будет генерировать до 14 делений с идентификатором ajax.
Поставьте открывающий тег div после if ( $the_query->have_posts() ) {
и закрывающий после wp_reset_postdata();
, и все будет в порядке.
Редактировать
Вы уверены, что не пропустили никаких открывающих/закрывающих тегов php? например, load_more_button() завернута в php, но вы не закрывали свой php раньше.
<?php
/*
* Template Name:
*/
get_header();
get_template_part ('inc/carousel');
$the_query = new WP_Query( [
'posts_per_page' => 14,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) { ?>
<div id="ajax">
<?php
$i = 0;
while ( $the_query->have_posts() ) { $the_query->the_post();
if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
<article <?php post_class( 'col-md-12' ); ?>>
<?php the_post_thumbnail('large-thumbnail'); ?>
<h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
<?php get_template_part( 'share-buttons' ); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
</article><?php
} else { // Small posts ?>
<article <?php post_class( 'col-md-4' ); ?>>
<?php the_post_thumbnail( 'medium-thumbnail' ); ?>
<h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
<?php get_template_part( 'share-buttons' ); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
</article>
<?php
}
$i++;
}?>
</div>
<?php if(get_query_var('paged') < $the_query->max_num_pages) {
load_more_button();
}
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();