存档-{CUSTOM_POST_TYPE}.php无法识别WordPress

时间:2014-08-31 作者:Ady

我已注册自定义帖子类型city 有2种分类法city_categoriescities. 但wordpress正在存档。php而不是archive city。php,不明白我错在哪里了。只有创建分类城市时,它才有效。php,然后是城市分类法。php。我怎样才能让它直接进入存档城市。php

register_post_type( \'city\',
    array(
        \'labels\' => 
            array(
            \'name\' => \'City Guides\',
            \'singular_name\' => \'City Guides\',
            \'add_new\' => \'Add New\',
            \'add_new_item\' => \'Add New retailer\',
            \'edit\' => \'Edit\',
            \'edit_item\' => \'Edit retailer\',
            \'new_item\' => \'New retailer\',
            \'view\' => \'View\',
            \'view_item\' => \'View retailer\',
            \'search_items\' => \'Search retailers\',
            \'not_found\' => \'No retailer found\',
            \'not_found_in_trash\' => \'No Shop retailer in Trash\',
            \'parent\' => \'Parent retailer\'
        ),
        \'public\' => true,
        \'menu_position\' => 6,
        \'supports\' => array( \'title\', \'editor\', \'thumbnail\' ),
        \'has_archive\' => true,
        \'taxonomies\' => array(\'city_categories\'),
        \'can_export\' => true,
        \'rewrite\' => array(\'slug\'=>\'travel\')
    )
);  

$args = array( 
    \'labels\' => $labels, // defined correctly, shortened the code
    \'public\' => true,
    \'show_in_nav_menus\' => true,
    \'show_ui\' => true,
    \'show_tagcloud\' => true,
    \'show_admin_column\' => true,
    \'hierarchical\' => true,
    \'rewrite\' => true,
    \'query_var\' => true
);

register_taxonomy( \'city_categories\', array(\'city\'), $args );

$args = array(
    \'labels\' => $labels, // defined correctly, shortened the code
    \'public\' => true,
    \'show_in_nav_menus\' => true,
    \'show_ui\' => true,
    \'show_tagcloud\' => true,
    \'show_admin_column\' => true,
    \'hierarchical\' => true,
    \'rewrite\' => true,
    \'query_var\' => true,
    \'sort\' => true
);

register_taxonomy( \'cities\', array(\'city\'), $args );

2 个回复
SO网友:cybmeta

我已经测试了您的代码,如果您在init 行动挂钩。在您的代码中,我没有看到注册post类型的操作挂钩。Cna你试试这个?(请记住在更新代码后刷新rewirte规则。请注意,我删除了未定义的变量$labels$args 分类法数组):

add_action(\'init\', \'cyb_register_post_type_and_taxonomies\');
function cyb_register_post_type_and_taxonomies() {
    register_post_type( \'city\',
                        array(
                            \'labels\' => 
                                array(
                                    \'name\' => \'City Guides\',
                                    \'singular_name\' => \'City Guides\',
                                    \'add_new\' => \'Add New\',
                                    \'add_new_item\' => \'Add New retailer\',
                                    \'edit\' => \'Edit\',
                                    \'edit_item\' => \'Edit retailer\',
                                    \'new_item\' => \'New retailer\',
                                    \'view\' => \'View\',
                                    \'view_item\' => \'View retailer\',
                                    \'search_items\' => \'Search retailers\',
                                    \'not_found\' => \'No retailer found\',
                                    \'not_found_in_trash\' => \'No Shop retailer in Trash\',
                                    \'parent\' => \'Parent retailer\'
                                ),
                            \'public\' => true,
                            \'menu_position\' => 6,
                            \'supports\' => array( \'title\', \'editor\', \'thumbnail\' ),
                            \'has_archive\' => true,
                            \'taxonomies\' => array(\'city_categories\'),
                            \'can_export\' => true,
                            \'rewrite\' => array(\'slug\'=>\'travel\')
                         )
    );
    $args = array( 
            \'labels\' => array(),
            \'public\' => true,
            \'show_in_nav_menus\' => true,
            \'show_ui\' => true,
            \'show_tagcloud\' => true,
            \'show_admin_column\' => true,
            \'hierarchical\' => true,
            \'rewrite\' => true,
            \'query_var\' => true
    );

    register_taxonomy( \'city_categories\', array(\'city\'), $args );

    $args = array(
            \'labels\' => array(), // defined correctly, shortened the code
            \'public\' => true,
            \'show_in_nav_menus\' => true,
            \'show_ui\' => true,
            \'show_tagcloud\' => true,
            \'show_admin_column\' => true,
            \'hierarchical\' => true,
            \'rewrite\' => true,
            \'query_var\' => true,
            \'sort\' => true
    );

    register_taxonomy( \'cities\', array(\'city\'), $args );
}

SO网友:Nicolai Grossherr

听起来您的重写规则仍有问题,请尝试添加flush_rewrite_rules() 在包含您在问题中发布的注册呼叫的函数末尾。像这样:

flush_rewrite_rules( false );
设置参数$hardfalse 表示您不会更新.htaccess 只需更新rewrite_rules 转瞬即逝的不过应该足够了。

But 不要忘记在测试后从代码中删除这一行,否则每次调用站点时都会执行这一行,这很糟糕,您当然不希望这样。

结束

相关推荐

显示Archives.php中的所有自定义帖子类型

我该怎么做?archive.php 只有以下内容:wp_get_archives(\'type=monthly\'); 以及wp_get_archives() 没有显示所有帖子类型的参数。我也认为archive-[post_type].php 不是我要找的,因为我希望所有帖子类型都显示在一个归档页面中。谢谢W