Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

php - Timber: Single post pagination does not work (wp_link_pages)

I am fairly new to Timber and I am trying to get the pagination for a single post item working. Since there are a lot of topics around the pagination for archives, I want to underline that this is about a single post pagination, that handles a multi-page single post created with <!--nextpage--> tags. In basic WordPress/PHP usage it is handled with wp_link_pages() inside the loop.

As far as I understood, by searching SO and other websites, the magic should happen by just using the post.pagination array. But it's empty inside my twig file (and according to the Timber documentation does not exist at all inside the TimberPost object).

My singular.php header looks like this:

$context     = Timber::get_context();
$post_object = new TimberPost();

And here is my twig file:

{% if post.pagination %}

    <nav>Pages:

        {% for page in post.pagination.pages %}

            {% if page.current %}

                <span class="current">{{ page.title }}</span>{{ loop.last ? '' : ', ' }}

            {% else %}

                <a href="{{ page.link }}">{{ page.title }}</a>{{ loop.last ? '' : ', ' }}

            {% endif %}

        {% endfor %}

    </nav>

{% endif %}

What am I doing wrong, what am I missing, how can I get the single post pagination to work with Timber?

question from:https://stackoverflow.com/questions/65845206/timber-single-post-pagination-does-not-work-wp-link-pages

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To make wp_link_pages() work as you expect add these lines to your singular.php

global $post, $page, $pages, $multipage;
setup_postdata( $post );

// setting up your context
$context         = Timber::context();
$context['post'] = new TimberPost();

if ( $multipage ) {
    $context['post']->post_content = $pages[$page - 1];
}

Then in your twig file instead of post.content you should write post.paged_content to get page content


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...