因此,我很难解决这个问题,也许有人会为我找到解决方案,我通过ajax点击加载更多帖子,我想在没有更多帖子可加载时隐藏“加载更多”按钮。我知道要实现这一点,需要比较哪些值,问题是它们在我的函数中。php文件和按钮位于我的模板文件中。如何将这些值传递给模板?提前感谢!
功能。php
function get_publications() {
if ( isset($_REQUEST) ) :
// global $maxpost,$mypage;
$args = array( \'post_type\' => \'publications\', \'posts_per_page\' => 2, \'offset\' => 2 );
$loop = new WP_Query( $args );
$mypage = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$maxpost = $loop->max_num_pages;
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<section class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 publications-section tagged-posts post">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php echo get_permalink( $post->ID ); ?>">
<div class="col-sm-4 col-sm-push-8 publications-section-image" style="background-image:url(\'<?php the_post_thumbnail_url(); ?>\');"></div>
</a>
<?php endif; ?>
<div class="col-sm-8 col-sm-pull-4">
<h2><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<small><?php echo get_post_meta($post->ID, "_contact", true); ?></small>
<small class="pub-tags loaded"> <span><?php the_terms( $post->ID,\'mytag\',\'#\',\' #\'); ?></span></small>
</div>
</section>
<?php echo ++$mypage; echo $maxpost; endwhile;
endif;
die();
}
add_action( \'wp_ajax_get_publications\', \'get_publications\' );
add_action( \'wp_ajax_nopriv_get_publications\', \'get_publications\' );
加载。js公司
$(document).ready(function(){
$(\'#loadmore\').click(function(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
$.ajax({
url: ajaxurl,
data: {
\'action\' : \'get_publications\',
},
success:function(data) {
$(\'#posts section:last-child\').after(data);
// console.log(max_post_vars.posts);
}
});
console.log(\'loadmore clicked\');
return false;
});
});
模板文件
$args = array(
\'post_type\' => \'publications\',
\'posts_per_page\' => 2,
);
$query = new WP_Query( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div id="posts">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<section class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 publications-section tagged-posts post">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php echo get_permalink( $post->ID ); ?>">
<div class="col-sm-4 col-sm-push-8 publications-section-image" style="background-image:url(\'<?php the_post_thumbnail_url(); ?>\');"></div>
</a>
<?php endif; ?>
<div class="col-sm-8 col-sm-pull-4">
<h2><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<small><?php echo get_post_meta($post->ID, "_contact", true); ?></small>
<small class="pub-tags pub"> <span><?php the_terms( $post->ID,\'mytag\',\'\',\' \'); ?></span></small>
</div>
</section>
<?php echo $mypage; endwhile; wp_reset_postdata(); ?>
</div>
<div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2" id="pub-btn-section">
<input type="submit" class="btn" id="loadmore" name="" value="<?php the_field(\'load_more\',pll_current_language(\'slug\')); ?>">
</div>
<?php endif; ?>