我在WordPress中使用Timber。
我正在尝试创建一个带有"Load more posts"按钮的帖子列表。
当用户单击"Load more posts"按钮时,我希望显示10个相同类别的帖子,并加载相同类别的其他10个帖子。
当没有类别之间的区别时,所有的工作都很好。按钮"Load more posts"工作正常。展示了10个员额。
但是,当我试图显示相同类别的文章时,我点击按钮"Load more posts"。没有张贴的帖子。类别有什么问题?
能帮我一下吗?
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$context['posts'] = Timber::get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => array($category),
'posts_per_page' => 10,
'paged' => 1,
'has_password' => FALSE
));function load_more_news() {
var page = 1;
$(document).on('click', '#load-more-news', function(e) {
e.preventDefault();
page++;
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'html',
data: {
'action' : 'get_news',
'get_page' : page
},
success: function(data) {
if($('').html(data).find('.archive__item.ended').size() > 0) $('#load-more-news').parents('.cta').remove();
else $('#load-more-news').parents('.cta').show();
$('#archive__list').append(data);
},
error: function(data) {
console.log(data);
}
});
});
}add_action( 'wp_ajax_nopriv_get_news', 'get_news' );
add_action( 'wp_ajax_get_news', 'get_news' );
function get_news() {
global $post;
$context = Timber::get_context();
$context['get_page'] = empty($_POST['get_page']) ? 1 : $_POST['get_page'];
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$context['posts'] = Timber::get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => array($category),
'posts_per_page' => 10,
'paged' => $context['get_page'],
'has_password' => FALSE
));
$count = count(Timber::get_posts(array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'category__in' => array($category)
)));
if($count <= $context['get_page'] * 10) $context['ended'] = 'ended';
Timber::render( 'bloc_news.twig', $context );
die();
} In the same thematic
...
{% if posts|length >= 10 %}
Load more posts
{% endif %}发布于 2020-08-19 11:13:02
我认为在您的代码中有一个小的更正,您只是传递页码,但您也有传递类别ID。我在load按钮中添加了data-category属性。因此,您的存档.like应该是:
In the same thematic
...
{% if posts|length >= 10 %}
Load more posts
{% endif %}通过script.js传递data-category属性值,因此script.js代码应该如下所示:
function load_more_news() {
var page = 1;
$(document).on('click', '#load-more-news', function(e) {
e.preventDefault();
page++;
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'html',
data: {
'action' : 'get_news',
'get_page' : page,
'get_category' : $(this).data('category'),
},
success: function(data) {
if($('').html(data).find('.archive__item.ended').size() > 0) $('#load-more-news').parents('.cta').remove();
else $('#load-more-news').parents('.cta').show();
$('#archive__list').append(data);
},
error: function(data) {
console.log(data);
}
});
});
}现在在服务器端(如data-category )接收get_page属性值,因此您的functions.php代码应该如下所示:
add_action( 'wp_ajax_nopriv_get_news', 'get_news' );
add_action( 'wp_ajax_get_news', 'get_news' );
function get_news() {
global $post;
$context = Timber::get_context();
$context['get_page'] = empty($_POST['get_page']) ? 1 : $_POST['get_page'];
$context['get_category'] = isset($_POST['get_category']) ? $_POST['get_category'] : '';
$context['posts'] = Timber::get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => array($context['get_category']),
'posts_per_page' => 10,
'paged' => $context['get_page'],
'has_password' => FALSE
));
$count = count(Timber::get_posts(array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'category__in' => array($context['get_category'])
)));
if($count <= $context['get_page'] * 10) $context['ended'] = 'ended';
Timber::render( 'bloc_news.twig', $context );
die();
}https://wordpress.stackexchange.com/questions/373299
复制相似问题