简体   繁体   中英

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.

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 All
  • 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.

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
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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