php - 默认情况下未选择自定义分类模板

标签 php wordpress

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

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 错误。请参阅The Codex 此示例用于在激活插件时创建新的自定义帖子类型。

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' );

关于php - 默认情况下未选择自定义分类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32088981/

相关文章:

php - 在两个表和 ID 中搜索查询

php - MySQL LEFT JOIN, INNER JOIN 等,复杂查询,PHP + MySQL for a forum

php - $_SERVER 变量没有设置我的自定义 header

css - Wordpress 主题问题中的 Bootstrap 网格框架

php - 在 Woocommerce 中下订单后,将值插入自定义表中

javascript - React - 使用 wp_mail 发送电子邮件

php - WordPress - 在 get_header 中包含变量不起作用

php - 在 Codeigniter 中调用未定义的函数 ibase_connect()

php - MySQL数据建模

javascript - 使用 Ajax 传递单击的提交按钮值