自定义发布类型存档(存档-{post-type}.php)不起作用

时间:2015-09-18 作者:Mayeenul Islam

这个问题有很多答案,甚至在这个网站上,但没有一个适合我。是的,我做了我所知道的所有事情,我从以前的答案中得到了答案,但我的自定义帖子类型存档不起作用:

<?php
function project_register_cpt_books() {

    $labels = array(
        \'name\'                  => __( \'Books\', \'textdomain\' ),
        \'singular_name\'         => __( \'Book\', \'textdomain\' ),
        \'name_admin_bar\'        => __( \'Books\', \'textdomain\' ),
        \'add_new\'               => __( \'Add New\', \'textdomain\' ),
        \'add_new_item\'          => __( \'Add New Book\', \'textdomain\' ),
        \'edit_item\'             => __( \'Edit Book\', \'textdomain\' ),
        \'new_item\'              => __( \'New Book\', \'textdomain\' ),
        \'view_item\'             => __( \'View Book\', \'textdomain\' ),
        \'search_items\'          => __( \'Search Books\', \'textdomain\' ),
        \'not_found\'             => __( \'No Book found\', \'textdomain\' ),
        \'not_found_in_trash\'    => __( \'No Book found in Trash\', \'textdomain\' ),
        \'parent_item_colon\'     => __( \'Parent Book:\', \'textdomain\' ),
        \'menu_name\'             => __( \'Books\', \'textdomain\' ),
    );

    $args = array(
        \'labels\'                => $labels,
        \'hierarchical\'          => false,
        \'description\'           => __( \'To get books information\', \'textdomain\' ),
        \'supports\'              => array( \'title\', \'editor\' ),
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 5,
        \'show_in_nav_menus\'     => true,
        \'publicly_queryable\'    => true,
        \'exclude_from_search\'   => false,
        \'has_archive\'           => true,
        \'query_var\'             => true,
        \'can_export\'            => true,
        \'rewrite\'               => array( \'slug\' => \'books\' ),
        \'capability_type\'       => \'post\'
    );
    register_post_type( \'books\', $args );

    /**
     * To Activate Custom Post Type Single page.
     * @author  Bainternet
     * @link http://en.bainternet.info/2011/custom-post-type-getting-404-on-permalinks
     * -----------
     */
    $set = get_option(\'post_type_rules_flased_books\');
    if ($set !== true){
        flush_rewrite_rules(false);
        update_option(\'post_type_rules_flased_books\',true);
    }

}
add_action( \'init\', \'project_register_cpt_books\' );
激活/停用主题、刷新永久链接、检查默认和PostName永久链接、更改slug、更改/注释rewrite 参数全部失败。我不可能archive-books.php 正在工作。现在又回到了archive.php 无论如何。

我怎样才能解决这个问题?

1 个回复
最合适的回答,由SO网友:Mayeenul Islam 整理而成

我发现了我的问题,因为我自己在不知不觉中造成了这个问题。如果类似的情况会导致问题,我将发布答案供您调试:

我使用以下代码将所有CPT条目包含到默认循环中。这就是问题的根源。它将posttype存档定向为默认post 存档(archive.php).

<?php
/**
 * Adding the Book entries to the loop.
 * @param object $query WordPress\' default query object.
 * ------------------------------------------------------------------------------
 */
function project_add_book_entries_to_query( $query ) {
    if ( $query->is_main_query() && !is_admin() ) {
        $query->set( \'post_type\', array(\'post\', \'books\') );
    }
}
add_action( \'pre_get_posts\', \'project_add_book_entries_to_query\' );

相关推荐