简体   繁体   中英

Timber Custom Post Type Archive returned 404 Pagination

I'm using Timber/Twig for this site I am working on. I've got an archive PHP file that renders the custom post type 'podcast' and creates the pagination, which appears on the archive posts page:

'localhost/podcasts/'

It produces the set amount of pages depending on how many posts per page I set. Ex. 10 posts total, 3 posts per page, renders 3 posts, and there are 4 pages of pagination. However, the links '/podcasts/page/2' or more return 404.

How can I get it to return the archive-template("podcast_cpt").twig with the next set of posts?

My Archive.php file looks like this:


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

$templates = array( 'archive.twig', 'index.twig' );

$context = Timber::get_context();

$podargs = array(
    // Get post type project
    'post_type' => 'sl_podcasts_cpts',
    'posts_per_page' => 3,
    'paged' => $paged,
    'page' => $paged,
    // Order by post date
    'orderby' => array(
        'date' => 'DESC'
    ));

$myCollection = new Timber\PostQuery( $podargs );
$context['podcasts'] = $myCollection;
$context['pagination'] = $myCollection->pagination();


$context['title'] = 'Archive';

if ( is_day() ) {
    $context['title'] = 'Archive: ' . get_the_date( 'D M Y' );
} else if ( is_month() ) {
    $context['title'] = 'Archive: ' . get_the_date( 'M Y' );
} else if ( is_year() ) {
    $context['title'] = 'Archive: ' . get_the_date( 'Y' );
} else if ( is_tag() ) {
    $context['title'] = single_tag_title( '', false );
} else if ( is_category() ) {
    $context['title'] = single_cat_title( '', false );
    array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
} else if ( is_post_type_archive() ) {
    $context['title'] = post_type_archive_title( '', false );
    array_unshift( $templates, '/archives/archive-' . get_post_type() . '.twig' );
}

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

Timber::render( $templates, $context );

And it renders in a twig file:

{% for podcast in podcasts %}
     {% include 'partials/podcast-player.twig' %}
{% endfor %}
{% include 'pagination.twig' with {'posts': podcasts} %}

I think you have an extra 'page' argument. You might need to check this out https://codex.wordpress.org/Pagination

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$podargs = array(
    'post_type' => 'sl_podcasts_cpts',
    'posts_per_page' => 3,
    'paged' => $paged,
    'orderby' => array(
        'date' => 'DESC'
    ));

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