![](/img/trans.png)
[英]Posts from specific WordPress category not displaying in Timber/Twig for loop
[英]Display all posts from parent category and group by child category with headings in Timber/Twig
我將以下代碼用於 select 並為我客戶網站上的類別登錄頁面設置上下文。
代碼
$args = array(
'cat' => '5,3,4,6',
'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
);
$context['stories'] = Timber::get_posts( $args );
我正在使用此代碼來設置模板並分配上下文。
代碼
if( $category->parent == 0 ) {
// Stories parent category
$templates = array( 'category.twig' );
$context['categories'] = $categories;
}
這一切正常,但我需要復制功能並進行以下調整。
numberofposts
設置為全部此布局將類似於“所有帖子”頁面。 該頁面將分配給一個名為“所有故事”的菜單項。
是否可以在無需編寫大量額外代碼的情況下完成這項工作? 我正在學習 Timber 和 Twig 和 go,所以請隨時分享一些提示和技巧來改進我的方法。
將帖子數設置為全部。
'numberposts' => -1,
更新上下文以添加標題。
$context['title'] = single_cat_title( '', false );
$context['description'] = category_description();
顯示分配給每個類別的所有帖子。
$cat_id = 1;
$context['category_posts'] = Timber::get_posts(
array(
'cat' => $cat_id,
'posts_per_page' => -1
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.