木材:archive-post_type.php不触发;与非木材主题作品相当

我正在使用Timber来呈现自定义帖子类型的自定义存档页面。 CPT是person,我只是在尝试访问“ mysite.com/person”时呈现 archive-person.php 。无论我做什么,我得到的只是 archive.php 的输出,而不是 archive-person.php

是的,我已经按许多人的建议在永久链接上点击了“保存”。

作为测试,使用WP的2020年基本主题,我以经典的WordPress方式制作了等效的 archive-post_type.php ,并且一切正常。这表明我的cpt和永久链接都可以,并且该问题可能与我基于Timber的主题有关。

我尝试更改CPT名称属性,以确保不会造成永久链接冲突。

木材视图/部分,页面,单张和 single-post_type.php 都可以正常显示。

除了修补Archive * .php页面之外,我应该首先看的是什么?在functions.php木材初始化中,有什么专业会专门影响档案吗?

更新:在撰写本文时,我看到了可能是一个问题。为了完全了解它们,我是否必须通过Timber init明确注册我的CPT和分类法?

archive.php(与Timber starter theme中的一个非常相似)

<?php

use Timber\Timber;

global $paged;

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

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

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

// Grabs everything on the site,regular posts and CPT's.
$context['posts'] = new \Timber\PostQuery();

    // Just for testing,to check if "person" posts can at least be rendered via archive.php. They do.
    //$args    = ['post_type' => 'person'];
    //$context['posts'] = new \Timber\PostQuery($args);

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

?>

archive-person.php 只是压缩版的archive.php,其中“ person”被强制设为post_type(而不是抓取所有帖子):

use Timber\Timber;

global $paged;

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

$templates = array( 'pages/archive-person.twig' );

$context = Timber::context();
$context['title'] = 'Archive: Person';

$args    = [
    'posts_per_page' => 10,'paged'          => $paged,'post_type' => 'person',];

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

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

functions.php

class mySite extends Timber\Site {
    public function __construct() {
        add_theme_support( 'post-thumbnails' );
        add_theme_support( 'menus' ); 

        add_filter( 'timber_context',array( $this,'add_to_context' ) );
        parent::__construct();
    }

    function add_to_context( $context ) {
        $context['menu'] = new \Timber\Menu('main-menu');
        $context['site'] = $this;

        return $context;
    }
}

new mySite();

function create_posttypes() {

      register_post_type( 'person',array(
                'labels' => array(
                    'name' => __( 'Persons' ),'singular_name' => __( 'Person' )
                ),'public' => true,'has_archive' => true,'show_in_rest' => true,'supports' => array( 'title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes' )
            )
        );

}

add_action( 'init','create_posttypes' );
michael2014 回答:木材:archive-post_type.php不触发;与非木材主题作品相当

您所做的对我来说似乎足够,但是将其添加到您的functions.php中,以强制标识您的新帖子类型

add_action('init','person_archive_rewrite');

function person_archive_rewrite(){
   add_rewrite_rule('^person/?','index.php?post_type=person','top');
}

然后点击保存永久链接

保持更新

,

您能否添加您编写的用于注册自定义帖子类型“人物”的代码?注册 CPT 时,默认存档设置 has_archive 为 false - 确保将其设置为 true。 https://developer.wordpress.org/reference/functions/register_post_type/#has_archive

本文链接:https://www.f2er.com/3033563.html

大家都在问