简体   繁体   English

显示来自父类别的所有帖子,并按子类别分组,标题为 Timber/Twig

[英]Display all posts from parent category and group by child category with headings in Timber/Twig

I am using the following code to select and set context for a category landing page on my client's website.我将以下代码用于 select 并为我客户网站上的类别登录页面设置上下文。

Code代码

$args = array(
  'cat' => '5,3,4,6',
  'numberposts' => 5,
  'orderby' => 'date',
  'order' => 'DESC',
);
$context['stories'] = Timber::get_posts( $args );

I am using this code to set the template and assign context.我正在使用此代码来设置模板并分配上下文。

Code代码

if( $category->parent == 0 ) {
  // Stories parent category
  $templates = array( 'category.twig' );
  $context['categories'] = $categories;
}

This is all working, but I am needing to duplicate the functionality and make the following adjustments.这一切正常,但我需要复制功能并进行以下调整。

  • Set numberofposts to Allnumberofposts设置为全部
  • Add headings for each of the children categories为每个子类别添加标题
  • Display all of the posts assigned to each category below the headings在标题下方显示分配给每个类别的所有帖子

This layout will act like an "all posts" page.此布局将类似于“所有帖子”页面。 This page will be assigned to a menu item called "All Stories".该页面将分配给一个名为“所有故事”的菜单项。

Is it possible to make this work without having to write lots of additional code?是否可以在无需编写大量额外代码的情况下完成这项工作? I am learning Timber and Twig as I go, so please feel free to share some tips and tricks for improving my approach.我正在学习 Timber 和 Twig 和 go,所以请随时分享一些提示和技巧来改进我的方法。

Set numberofposts to all.将帖子数设置为全部。

 'numberposts'       => -1,

Update context toadd headings.更新上下文以添加标题。

$context['title']       = single_cat_title( '', false );
$context['description'] = category_description();

Display all of the posts assigned to each category.显示分配给每个类别的所有帖子。

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM