[英]Timber posts_per_page only showing one post on archive
我有一個存檔頁面,它沒有按預期顯示所有結果。 在我的查詢中將 post_per_page 參數更改為 -1 確實允許顯示所有結果,但是當它限制為 5 個帖子時,我只收到一個結果:
if (!isset($paged) || !$paged){
$paged = 1;
}
$context = Timber::get_context();
$course_args = array(
'post_type' => 'course',
'posts_per_page' => 4,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'course-category',
'field' => 'ID',
'terms' => $cat
)
)
);
$context['pagination'] = Timber::get_pagination();
$context['post'] = Timber::get_posts($course_args);
我認為的基本迭代
{% for course in post %}
<div class="col-md-6 px-3 py-5">
//courseCard
</div>
{% endfor %}
任何幫助將不勝感激,因為現在我一直在看它,以至於我一直在兜兜轉轉。
嘗試使用Timber\\PostQuery
$context = Timber::get_context();
$course_args = array(
'post_type' => 'course',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'course-category',
'field' => 'ID',
'terms' => $cat // I guess this variable was created before
)
)
);
$context['posts'] = new Timber\PostQuery($course_args);
// Render twig
您不需要創建分頁,因為 PostQuery 會在您的樹枝文件中處理
{% for course in posts %}
<div class="col-md-6 px-3 py-5">
//courseCard
</div>
{% endfor %}
{% include 'partials/pagination.twig' with {pagination: posts.pagination} %}
您的pagination.twig應包含有關分頁的信息
{{ dump(pagination) }}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.