简体   繁体   中英

Custom Post Type archive page not being used in Timber/Twig WordPress setup

The problem: our WordPress installation is using archive.php for an archive page instead of the desired archive-article.php.

Here is our configuration:

functions.php

register_post_type(
            'article',
            array(
                'labels' => array(
                    'name' => __('Articles'),
                    'singular_name' => __('Article'),
                ),
                'public' => true,
                'has_archive' => true,
                'menu_icon' => 'dashicons-media-default',
                'menu_position' => 4,
                'taxonomies' => array('article_type'),
                'supports' => array('title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail')
            )
        );

archive-article.php

<?php
/**
 * The template for displaying Archive pages.
 *
 * Used to display archive-type pages if nothing more specific matches a query.
 * For example, puts together date-based pages if no date.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.2
 */

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

$context = Timber::context();

$args = array(
    'post_type'   => 'article',
    'posts_per_page' => '-1'
);

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

$context['categories'] = $post->terms( array(
    'query' => [
        'taxonomy' => 'article_type',
    ],
) );

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

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

Any help from a second pair of eyes would be greatly appreciated!

除了 has_archive 之外,尝试将存档 slug 添加到您的自定义帖子注册?

"rewrite" => array("slug" => "articles"),

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