默认情况下不选择自定义分类模板 [英] Custom taxonomy template is not selected by default

查看:26
本文介绍了默认情况下不选择自定义分类模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册了一个自定义帖子类型 faq 和一个名为 cat_faqfaq 分类法.我创建了一个名为 taxonomy-cat_faq.php 的模板,但默认情况下它没有被选中.它始终显示 404 页面.我不明白为什么会这样?我读出了 Template Hierarchy 并按如下方式工作但未能找出错误.我附上了下面的代码

I have register a custom post type faq and a taxonomy for faq named cat_faq. I created a template named taxonomy-cat_faq.php, but it is not selected by default. It always displays a 404 page. I could not understand why this happens so? I read out the Template Hierarchy and worked as follows but failed to find out the error. I have attached the code below

add_action('init', 'register_faq');
function register_faq(){
    $post_type = 'faq';
    $label = array(
        'name'          => _x('FAQ', 'FAQ', 'faq'),
        'singular_name' => _x('FAQ', 'FAQ', 'faq'),
        'menu_name'     => _x('FAQ', 'admin menu', 'faq')
        );
    $args = array(
        'labels'        => $label,
        'public'    => true,
        'rewrite'   => array('slug' => 'faq'),
        'capability'    => 'post',
        'supports'      => array('title', 'editor', 'thumbnail'),
    );
    register_post_type($post_type, $args);
}

// create taxonomies for the post type "faq"
add_action( 'init', 'create_faq_taxonomies', 0 );
function create_faq_taxonomies() {
    $labels = array(
        'name'              => _x( 'FAQ', 'taxonomy general name' ),
        'singular_name'     => _x( 'FAQ', 'taxonomy singular name' ),
        'search_items'      => __( 'Search FAQ' ),
        'edit_item'         => __( 'Edit FAQ' ),
        'update_item'       => __( 'Update FAQ' ),
        'add_new_item'      => __( 'Add New FAQ' ),
        'new_item_name'     => __( 'New FAQ' ),
        'menu_name'         => __( 'Category FAQ' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'cat_faq' ),
    );

    register_taxonomy( 'cat_faq', array( 'faq' ), $args );
}   

推荐答案

最好以这种方式制作自定义帖子类型.flush_rewrite_rules() 将治愈 404.请参阅法典此示例用于在激活插件时创建新的自定义帖子类型.

It's better to make your custom post types in this fashion. The flush_rewrite_rules() will cure the 404's. See The Codex This example is for creating new custom post types on the activation of your plugin.

add_action( 'init', 'my_cpt_init' );
function my_cpt_init() {
    register_post_type( ... );
}

function my_rewrite_flush() {
    // First, we "add" the custom post type via the above written function.
    // Note: "add" is written with quotes, as CPTs don't get added to the DB,
    // They are only referenced in the post_type column with a post entry, 
    // when you add a post of this CPT.
    my_cpt_init();

    // ATTENTION: This is *only* done during plugin activation hook in this example!
    // You should *NEVER EVER* do this on every page load!!
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );

这篇关于默认情况下不选择自定义分类模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆