【发布时间】:2018-01-05 15:24:53
【问题描述】:
我正在为我的网站使用预制的 wordpress 主题。但是,我想制作一个自定义的front-page.php,所以我做了,但现在的问题是我不知道如何向它添加ajax 加载更多按钮。我的主题已经使用了 ajax 加载更多按钮,所以我认为添加起来很简单。但我想我可能在错误的地方添加了代码,或者我的查询搞砸了?
谁能帮我添加这个加载更多按钮?
我的自定义front-page.php
<?php
get_header();
get_template_part ('inc/carousel');
$the_query = new WP_Query( [
'posts_per_page' => 13,
'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
}
get_footer();
我在主题中找到的 post-nav.php 代码
<div class="row pagination-below"><div class="col-md-12">
<?php
$pagination_type = novablog_getVariable('pagination_type') ? novablog_getVariable('pagination_type') : 'pagnum';
if($pagination_type=='pagnum') :
the_posts_pagination( array(
'mid_size' => 3,
'type' => 'list',
'prev_text' => theme_locals("prev"),
'next_text' => theme_locals("next")
) );
endif;
global $wp_query;
if ( $wp_query->max_num_pages > 1 && $pagination_type=='paglink' ) : ?>
<div class="paglink">
<span class="pull-left"><?php previous_posts_link(theme_locals("newer")) ?></span>
<span class="pull-right"><?php next_posts_link(theme_locals("older")) ?></span>
</div>
<?php endif; ?>
<?php
if ( $wp_query->max_num_pages > 1 && $pagination_type=='loadmore' or $wp_query->max_num_pages > 1 && $pagination_type=='infinite' ) {
$all_num_pages = $wp_query -> max_num_pages;
$next_page_url = novablog_next_page($all_num_pages);
?>
<div class="ajax-pagination-container">
<a href="<?php echo esc_url($next_page_url); ?>" id="ajax-load-more-posts-button"></a>
</div>
<?php } ?>
</div></div>
我希望我的首页帖子布局的示例。一行 1 个帖子,一行 2 个帖子,每行 2 个帖子,一行 1 个帖子,依此类推。然后每 15 个帖子后会出现加载更多按钮。
【问题讨论】:
-
如果你检查元素会发生什么?有什么错误吗?
-
我没有收到任何错误。
-
user6738171 考虑这个rudrastyh.com/wordpress/load-more-posts-ajax.html
-
你能发布你的 jquery 吗?
-
@vel 这是主题文件的链接 - dropbox.com/s/yghxnsvnbv3nnrh/novablog.zip?dl=0
标签: php html ajax wordpress pagination