所以我决定使用“加载更多”点击选项,但我使用的代码不起作用,点击“加载更多”,什么都不会发生。
指数php:
<div id="content">
<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(
\'multisite\' => 1,
\'sites__not_in\' => array(1),
\'paged\' => $paged,);
$loop = new WP_Query($args); ?>
<?php while( $loop->have_posts() ): $loop->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
<a id="more_posts" href="#">Load More</a>
<?php wp_reset_postdata(); ?>
</div>
在索引的底部。php:
var ajaxUrl = "<?php echo admin_url(\'admin-ajax.php\')?>";
var page = 1; // What page we are on.
var ppp = 3; // Post per page
$("#more_posts").on("click",function(){ // When btn is pressed.
$("#more_posts").attr("disabled",true); // Disable the button, temp.
$.post(ajaxUrl, {
action:"more_post_ajax",
offset: (page * ppp) + 1,
ppp: ppp
}).success(function(posts){
page++;
$("#content").append(posts); // CHANGE THIS!
$("#more_posts").attr("disabled",false);
});
});
还有我的功能。php:
function more_post_ajax(){
$offset = $_POST["offset"];
$ppp = $_POST["ppp"];
header("Content-Type: text/html");
$args = array(
\'posts_per_page\' => $ppp,
\'offset\' => $offset,
);
$loop = new WP_Query($args);
while ($loop->have_posts()) { $loop->the_post();
the_content();
}
exit;
}
add_action(\'wp_ajax_nopriv_more_post_ajax\', \'more_post_ajax\');
add_action(\'wp_ajax_more_post_ajax\', \'more_post_ajax\');
非常感谢您的帮助。