首页使用Timber后,每页使用不同的帖子?

时间:2018-06-12 04:13:20

标签: wordpress timber

关于博客如何分页,我在将主题迁移到Timber时遇到了问题。在博客的第一页上,我想要3个帖子,之后每个页面都以6个为一组继续。(第1页为1,2,3;第2页为4,5,6,7,8,9;第3页为10,11,12,13,14,15;等......)

我目前有11个帖子,' 1'是最古老的' 11'是最近的。第一页是11,10,9,显示它限制为3.然而,下一页返回显示5,4,3,2,1,当它应该是8,7,6,5,4,3时另一个链接再回过头来。

这就是我的blog.php中的内容:

<?php
/* Template Name: Blog */
global $paged;
if (!isset($paged) || !$paged) {
    $paged = 1;
}

if ($paged == 1) {
    $posts_per_page = 3;
} else {
    $posts_per_page = 6;
}

$context = Timber::get_context();
$context['posts'] = new Timber\PostQuery(array(
    'post_type' => 'post',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged
));
Timber::render('pages/blog.twig',$context);
?>

我的枝条文件:

{% extends 'base.twig' %}

{% block main %}
{% for post in posts %}
    <article>
        <a href="{{ post.link }}">
            <div class="text">
                <h1>{{ post.title }}</h1>
                <div class="excerpt">{{ post.content|truncate(40) }}</div>
            </div>
        </a>
    </article>
{% endfor %}

<section class="pagination">
    {% if posts.pagination.next %}
        <a href="{{ posts.pagination.next.link }}" class="prev"><span></span>older</a>
    {% endif %}
    {% if posts.pagination.prev %}
        <a href="{{ posts.pagination.prev.link }}" class="next">newer<span></span></a>
    {% endif %}
</section>
{% endblock %}

您使用的是什么版本的WordPress,PHP和Timber?

Wordpress 4.9.6 木材1.7.1 PHP 7.2.1

你是如何安装木材的?

通过WordPress仪表板中的插件更新程序升级到最新版本

更新1

我尝试修改它以使用pre_get_posts,但我不确定它是如何组合在一起的,以使分页继续另一页显示剩余的&#39; 2,1&#39;讯息。

<?php
/* Template Name: Blog */
global $paged;
if (!isset($paged) || !$paged) {
    $paged = 1;
}

if ($paged == 1) {
    $posts_per_page = 3;
} else {
    $posts_per_page = 6;
}

add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {
    //Before anything else, make sure this is the right query...
    if ( ! $query->is_home() ) {
        return;
    }

    //First, define your desired offset...
    if (!isset($paged) || !$paged) {
        $paged = 1;
    }
    if ($paged == 1) {
        $offset = 0;
    } else {
        $offset = 3;
    }

    //Next, determine how many posts per page you want (we'll use WordPress's settings)
    if ($paged == 1) {
        $ppp = 3;
    } else {
        $ppp = 6;
    }

    //Next, detect and handle pagination...
    if ( $query->is_paged ) {
        //Manually determine page query offset (offset + current page (minus one) x posts per page)
        $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
        //Apply adjust page offset
        $query->set('offset', $page_offset );
    }
    else {
        //This is the first page. Just use the offset...
        $query->set('offset',$offset);
    }
}

add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
    //Define our offset again...
    if (!isset($paged) || !$paged) {
        $paged = 1;
    }
    if ($paged == 1) {
        $offset = 0;
    } else {
        $offset = 3;
    }

    //Ensure we're modifying the right query object...
    if ( $query->is_home() ) {
        //Reduce WordPress's found_posts count by the offset... 
        return $found_posts - $offset;
    }
    return $found_posts;
}

$context = Timber::get_context();
$context['posts'] = new Timber\PostQuery(array(
    'post_type' => 'post',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged
));
$context['paged'] = $paged;
Timber::render('pages/blog.twig',$context);
?>

1 个答案:

答案 0 :(得分:0)

您需要做的是实际使用offset参数,但这会打破您的分页。

请参阅此https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination文档。

虽然首选的方法类似于文档中的方法,但您仍然可以使用您的示例。这样的事情可能有用:

if ($paged == 1) {
    $posts_per_page = 3;
    $offset = 0;
} else {
    $posts_per_page = 6;
    $offset = 3 + ($paged-2)*6; 
}

$context = Timber::get_context();
$context['posts'] = new Timber\PostQuery(array(
    'post_type' => 'post',
    'posts_per_page' => $posts_per_page,
    'offset' => $offset
));