【问题标题】:Timber Custom Post Type Archive returned 404 Pagination木材自定义帖子类型存档返回 404 分页
【发布时间】:2019-03-12 20:43:24
【问题描述】:

我正在为我正在开发的这个网站使用 Timber/Twig。我有一个存档 PHP 文件,它呈现自定义帖子类型“播客”并创建分页,它出现在存档帖子页面上:

'本地主机/播客/'

它会根据我设置的每页帖子的数量生成设置的页面数量。前任。一共10篇文章,每页3篇文章,渲染3篇文章,分页4页。但是,“/podcasts/page/2”或更多链接返回 404。

我怎样才能让它在下一组帖子中返回 archive-template("podcast_cpt").twig?

我的 Archive.php 文件如下所示:


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 );

它呈现在一个树枝文件中:

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

【问题讨论】:

  • 注册自定义帖子类型后,您是否刷新了永久链接设置?
  • 是的!我已经把它们冲洗了好几次。还是没有运气。

标签: php wordpress pagination twig timber


【解决方案1】:

我认为您有一个额外的“页面”参数。你可能需要检查一下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'
    ));

【讨论】:

  • 无论有无我都有过。我认为该行是一个保留,已被删除,代码没有任何改进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 2020-03-22
  • 2018-04-10
  • 2014-09-29
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
相关资源
最近更新 更多