0
votes

I am kinda a newbie at Wordpress. I've looked a lot on StackOverflow for a solution for this problem but none worked for me. I'm using Timber and ACF.

Here's the situation: I've made 4 custom post_type ( News, Products, Services and Places ). I've created a custom page for the news. (page-news.twig )

In my page.php file I've added a custom query like this:

global $paged;

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

$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;

$args = array(
    'post_type' => 'news',
    'posts_per_page' => 3,
    'paged' => $paged,
    'page' => $paged
);

$context['news'] = new Timber\PostQuery($args);

So I've set a maximum of posts for page, which is 3, and In my .twig file:

    {% for single_news in news %}
{% include['news.twig', single_news] %}
{% endfor %}

{% if news.pagination.prev %}
<a href="{{news.pagination.prev.link}}" class="prev {{ news.pagination.prev.link|length ? '' : 'invisible'}}">Prev</a>
{% endif %}

{% if news.pagination.next %}
<a href="{{ news.pagination.next.link}}" class="next {{ news.pagination.next.link|length ? '' : 'invisible'}}">Next</a>
{% endif %}

When I go to the news page, there are 3 posts per page ( correct ) and it displays me the Next anchor which is correct. The problem is that the link brings me to /news/page/2 that doesn't exists and gives me 404 error. I've tried many solutions like the pre_get_posts and none worked for me.

1

1 Answers

2
votes

Pagination only works on archive pages, but not on singular pages. See The WordPress Template Hierarchy for what counts as an archive page.

This means that you can’t use page.php as your template file for displaying your news posts. You have two possibilities here:

  1. You can use archive.php, or even archive-news.php if your Custom Post Type for news is named news.
  2. If you’d use the default post type post for your news section, you could select the page where you want to display your posts archive. The template file to display your posts archive would then be home.php.

If you go for the first option, you need to make sure that when you register your post type (either through register_post_type() or a plugin), you set the public option to true and that you set has_archive to true as well.

The problem with archive pages is that now you can’t use some editing functionalities that you normally have with pages. For an archive, you can’t set a title or add content with the editor like you’re used for pages. But if you use ACF, you can set up an options page where you add a select for your «Page for News». Name it page_for_news and then, in your template file, you can select the proper post that Timber should use on the archive page with

$post = new Timber\Post( get_field( 'page_for_news', 'option' ) );

This method practically replicates option 2 for Custom Post Types.

Also, a hint about your news post include in Twig:

{% for single_news in news %}
    {% include['news.twig', single_news] %}
{% endfor %}

If you do this, then Twig will try to find a news.twig template as well as a template named after single_news. But single_news is not a string here, but an instance of Timber\Post. So that doesn’t help you. You can use the following instead:

{% for single_news in news %}
    {% include 'news.twig' %}
{% endfor %}