Ad

Timber Twig Template - Sticky Post Doesn't Just Push To Top Of List, It Adds Extra Teaser To The Page

- 1 answer

I have code (below) which adds 12 post/page: 6 with a partial template that makes those 6 bigger; and 6 that get a template making those smaller - this works fine, until one of the posts is a sticky, then the sticky correctly goes to the top of the list, but there are now 7 of the smaller posts.

PHP file:

<?php
/**
 * Template Name: Blog Landing Page
 * Template Post Type: page
 */

global $paged;
  if (!isset($paged) || !$paged){
      $paged = 1;
  }

 $context = Timber::context();

$timber_post     = new Timber\Post();
$context['post'] = $timber_post;

$newsArgs   =   array(
  'post_type'     =>  'post',
  'post_status'     => 'publish',
  'posts_per_page'  =>  6,
  'paged'           => $paged,
  'orderby'       =>  array(
    'date'          =>  'DESC'
));
$context['news'] = new Timber\PostQuery($newsArgs);

Timber::render( 'layouts/layout-blog.twig', $context );

And Twig:

{% for post in news|slice(0,6) %}
  {% include "partial/teaser-article.twig" %}
{% endfor %}
{% for post in news|slice(6,12) %}
    {% include "partial/teaser-article-small.twig" %}
{% endfor %}
{% include 'partial/pagination.twig' with { pagination: news.pagination({show_all: false, mid_size: 1, end_size: 1}) } %}

Any suggestions greatly appreciated!

Ad

Answer

Twig's slice takes two parameters - start and length:

https://twig.symfony.com/doc/3.x/filters/slice.html

Therefore, instead of slice(6,12) you probably want slice(6,6)

Ad
source: stackoverflow.com
Ad