【发布时间】:2017-04-21 18:17:09
【问题描述】:
JQUERY 更新
<script type="text/javascript">
jQuery(function($){
function fetchBlogPosts(){
$.post( ajaxUrl, {'action' : 'post_blog',
'security' :'<?php echo wp_create_nonce('load_more_posts'); ?>' },
function(response){
});
}
$('.load-more').click(function(){
fetchBlogPosts();
});
});
</script>
PHP 函数更新
add_action('wp_ajax_post_blog', 'blog_post_data_fetch');
add_action('wp_ajax_nopriv_post_blog', 'blog_post_data_fetch');
function blog_post_data_fetch(){
check_ajax_referer('load_more_posts', 'security');
ob_clean();
get_template_part( 'inc/blog','posts' );
wp_die();
}
在阅读了几个 AJAX 教程之后,老实说 AJAX 与 WordPress 仍然让我感到困惑 :( 我坚持使用以下确实有效的代码。
但是,我只想将其他帖子添加到现有循环中,而不是重复相同的帖子。
因此,换句话说,初始页面加载下面的循环,然后当我单击“.load-more”按钮时,它应该通过 AJAX 加载更多帖子,但会被已经显示的现有帖子抵消。
这怎么可能?
FUNCTIONS.PHP
add_action('wp_ajax_post_blog', 'blog_post_data_fetch');
add_action('wp_ajax_nopriv_post_blog', 'blog_post_data_fetch');
function blog_post_data_fetch(){
get_template_part('inc/blog','posts');
}
博客帖子模板
<?php
$the_query = new WP_Query( array(
'post_type' => 'blog',
));
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="carousel-cell">
<div class="blog-item">
<div class="featured-image">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail('blog-thumb');
else :
?>
<img src="<?php bloginfo('template_url'); ?>/img/blog-thumb.jpg" alt="">
<?php endif; ?>
</a>
</div><!--/featured-image-->
<div class="post">
<h1><a href="#"><?php the_title(); ?>hello</a></h1>
<?php the_excerpt(); ?>
</div><!--/post-->
</div><!--/blog-item-->
</div><!--/carousel-cell-->
<?php endwhile; endif; ?>
JQUERY
function fetchBlogPosts(){
$.post( ajaxUrl, { 'action' : 'post_blog' }, function(response){
});
}
$('.load-more').click(function(){
fetchBlogPosts();
});
【问题讨论】:
标签: php jquery ajax wordpress loops