找不到自定义分类归档模板

时间:2015-10-21 作者:gvanto

我已经按照下面的代码创建了一个自定义分类法注册,但它没有找到分类法专业。php或存档。php模板(页面未找到错误)。

我做了以下工作(按照SE和web上其他线程的建议):-重新保存永久链接-在wp admin中查找分类设置页面,当我将鼠标悬停在“视图”上时,它会将我指向“mysite”。com/events/specialties/footology”,用于我为“event”类型的帖子创建的“footology”分类条目,因此URL与预期一致。但是当访问链接时仍然会出现“获取页面未找到”错误。

$specialty_args = array(
            \'hierarchical\' => false,
            // This array of options controls the labels displayed in the WordPress Admin UI
            \'labels\' => array(
                \'name\' => \'Specialty\',
                \'singular_name\' => \'Specialty\',
                \'search_items\' => \'Search specialties\',
                \'all_items\' => \'All specialties\',
                \'edit_item\' => \'Edit specialty\',
                \'update_item\' => \'Update specialty\',
                \'add_new_item\' => \'Add new specialty\',
                \'new_item_name\' => \'New specialty name\',
                \'menu_name\' => \'Specialties\',
            ),

            \'show_ui\' => true,
            \'show_admin_column\' => true,
            \'query_var\' => \'specialty\',

            // Control the slugs used for this taxonomy
            \'rewrite\' => array(
                \'slug\' => \'events/specialties\', // This controls the base slug that will display before each term
                \'with_front\' => false, // Don\'t display the category base before "/specialties/"
                \'hierarchical\' => false
            ),
        );

        register_taxonomy(\'specialty\', \'event\', $specialty_args);
        register_taxonomy_for_object_type(\'specialty\', \'event\'); //says in https://codex.wordpress.org/Function_Reference/register_taxonomy under \'Usage\' to do this
我不知道怎么了。感谢您的帮助!

2 个回复
SO网友:cybmeta

页面未找到错误并不意味着未找到/使用模板文件。请注意,URL返回404状态标头(未找到),然后返回404。使用php(如果404.php不存在,则使用index.php)。

我认为您真正的问题是,在分类法注册之后,您没有刷新重写规则。要执行此操作,请执行以下步骤:

Manually: 转到设置->永久链接并单击保存按钮(无需更改任何内容,只需单击保存按钮)。

Auto: 在插件中,使用flush_rewrite_rules() 插件激活期间挂钩(切勿使用flush_rewrite_rules() 每次加载页面时)。例如:

register_activation_hook( __FILE__, \'cyb_plugin_activation\' );
function cyb_plugin_activation() {

    cyb_register_taxonomy();
    flush_rewrite_rules();

}

add_action( \'init\', \'cyb_register_taxonomy\' );
function cyb_register_taxonomy() {

    $specialty_args = array(
            \'hierarchical\' => false,
            // This array of options controls the labels displayed in the WordPress Admin UI
            \'labels\' => array(
                \'name\' => \'Specialty\',
                \'singular_name\' => \'Specialty\',
                \'search_items\' => \'Search specialties\',
                \'all_items\' => \'All specialties\',
                \'edit_item\' => \'Edit specialty\',
                \'update_item\' => \'Update specialty\',
                \'add_new_item\' => \'Add new specialty\',
                \'new_item_name\' => \'New specialty name\',
                \'menu_name\' => \'Specialties\',
            ),

            \'show_ui\' => true,
            \'show_admin_column\' => true,
            \'query_var\' => \'specialty\',

            // Control the slugs used for this taxonomy
            \'rewrite\' => array(
                \'slug\' => \'events/specialty\', // This controls the base slug that will display before each term (renamed to specialty from specialties)
                \'with_front\' => false, // Don\'t display the category base before "/specialties/"
                \'hierarchical\' => false
            ),
        );

        register_taxonomy(\'specialty\', \'event\', $specialty_args);
        register_taxonomy_for_object_type(\'specialty\', \'event\');

}

register_deactivation_hook( __FILE__, \'cyb_plugin_deactivation\' );
function cyb_plugin_deactivation() {

    // Flush the rewrite rules also on deactivation
    flush_rewrite_rules();

}

SO网友:Por

\'show_ui\'           => true,
\'show_admin_column\' => true,
\'query_var\'         => \'specialty\',
\'has_archive\'       => true, //make sure you have added this one
您的归档文件名是什么?您的存档url是什么?实际上,您的归档文件名应该与归档专业类似。php,您的帖子类型归档url将类似于yourdomain。com/事件/专业。我已经测试过了,应该对你的有效。

如果您不确定自定义帖子类型,请使用wordpress type plugin 或使用wordpress generator

相关推荐