【发布时间】:2015-08-17 08:12:39
【问题描述】:
我在 WP 上有网站。
我正在使用 AJAX 来显示我的帖子。单击“更多”按钮会出现另外两个帖子。当显示所有帖子时,我试图隐藏按钮。但是出了点问题。
我不明白如何将帖子数计入 JS(这是另一种变体),因为构造 var posts_count = "<?php echo $posts_count; ?> 不起作用 - 它只提供字符串!
JS
$(function () {
var posts = 2; //posts shown
var posts_offset = 2;
$("#load-post").click(function (e) {
e.preventDefault();
$.ajax({
type: "GET",
url: "/wp-admin/admin-ajax.php",
data: ({
action: 'loadMore',
posts_offset: posts_offset
}),
success: function (data) {
$('.news #content').append(data);
posts_offset += 2;
}
});
});
});
PHP
function loadMore() {
$posts_count = wp_count_posts()->publish;
$posts_offset = $_GET['posts_offset'];
// get your $_GET variables sorted out
// setup your query to get what you want
query_posts( array( 'posts_per_page' => 2, 'offset'=> $posts_offset ) );
// initialsise your output
$output = '';
// the Loop
while (have_posts()) : the_post();
?><div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 news-item">
<div class="news-date">
<?php the_time($format = "j F Y") ?>
</div>
<div>
<h3><?php the_title() ?></h3>
<p><?php the_content() ?></p>
</div>
</div><?php
endwhile;
if ($posts_count - ($posts_offset + 2) <= 0) { ?>
<script>$("#load-more").hide();</script>
<?php }
// Reset Query
wp_reset_query();
die($output);
}
此代码有效,但在隐藏按钮部分无效
【问题讨论】:
-
你没有回显脚本部分
标签: javascript php jquery ajax wordpress