WordPress自定义文章类型模板archive-hob-ap.php不生效求助
问题:自定义文章类型归档页404且单篇文章不加载自定义模板
我使用WordPress 6.0.1 + Twenty Twenty-One主题开发插件,通过自定义文章类型(custom post type)和自定义分类法(custom taxonomy)展示图片及相关信息。为了在前端展示所有自定义文章列表,且确保任意主题下都能单独展示单篇文章,我按照WordPress模板层级规则创建了archive-hob-ap.php和single-hob-ap.php模板。但刷新固定链接后,访问mysite/hob-ap出现404错误;访问单篇文章mysite/hob-ap/single-post-title时,加载的是默认模板而非我自定义的模板。
自定义文章类型与分类法代码
function hob_post_type() { $labels = array( 'name' => _x( 'HOB Publicités', 'Post Type General Name'), 'singular_name' => _x( 'HOB Publicité', 'Post Type Singular Name'), 'menu_name' => __( 'HOB Publicité'), 'all_items' => __( 'Toutes les publications'), 'view_item' => __( 'Voir les publications'), 'add_new_item' => __( 'Ajouter une nouvelle publication'), 'add_new' => __( 'Ajouter une publication'), 'edit_item' => __( 'Editer la publication'), 'update_item' => __( 'Modifier la publication'), 'insert_into_item' => __('Insérer dans HOB Publication'), 'uploaded_to_this_item' => __('Uploader la publication'), 'search_items' => __( 'Rechercher une publication'), 'not_found' => __( 'Non trouvée'), 'not_found_in_trash' => __( 'Non trouvée dans la corbeille'), ); $args = array( 'label' => __( 'HOB Publicité'), 'description' => __( 'Organisez vos PDF protégés'), 'menu_icon' => plugins_url('/asset/img/logo.png', __FILE__), 'labels' => $labels, 'supports' => array( 'title', 'custom-fields'), 'show_in_rest' => true, 'hierarchical' => false, 'public' => true, 'has_archive' => true, // 'rewrite' => array( 'slug' => 'hobpage'), ); register_post_type( 'hob-ap', $args ); } add_action( 'init', 'hob_post_type', 0 ); add_action( 'init', 'hob_add_taxonomies', 0 ); function hob_add_taxonomies() { $args_registry = array( 'hierarchical' => false, 'label' =>__( 'N° de registre'), 'show_ui' => true, 'show_in_menu' => false, 'show_in_rest' => true, 'show_admin_column' => true, 'query_var' => true, 'meta_box_cb' => 'hob_add_register_number', 'rewrite' => array( 'slug' => 'hob_register' ), ); register_taxonomy( 'hob_register', 'hob-ap', $args_registry ); }
已尝试的操作
- 取消注释
rewrite参数修改slug,并将归档模板重命名为archive-hobpage.php - 调整
has_archive参数为true或false - 后台刷新固定链接
解决方案
1. 正确配置自定义文章类型的rewrite规则
自定义文章类型slug含连字符,需确保rewrite参数配置完整,避免重写规则冲突:
'rewrite' => array( 'slug' => 'hob-ap', 'with_front' => false, // 避免继承固定链接前缀,防止路径冲突 'pages' => true, // 启用分页功能 'feeds' => true // 可选,启用RSS feed ),
注意:模板文件名必须与slug对应——slug为hob-ap时,归档模板是archive-hob-ap.php,单篇模板是single-hob-ap.php。
2. 插件内通过钩子指定模板路径
插件开发中,不能依赖主题目录加载模板,需用template_include钩子强制加载插件内的模板:
function hob_ap_template_include( $template ) { // 处理归档页 if ( is_post_type_archive( 'hob-ap' ) ) { $custom_template = plugin_dir_path( __FILE__ ) . 'archive-hob-ap.php'; if ( file_exists( $custom_template ) ) { return $custom_template; } } // 处理单篇文章 if ( is_singular( 'hob-ap' ) ) { $custom_template = plugin_dir_path( __FILE__ ) . 'single-hob-ap.php'; if ( file_exists( $custom_template ) ) { return $custom_template; } } return $template; } add_filter( 'template_include', 'hob_ap_template_include' );
将这段代码添加到插件主文件,同时确保模板文件放在插件根目录(若模板在子目录,需修改路径,比如plugin_dir_path(__FILE__) . 'templates/archive-hob-ap.php')。
3. 插件激活/停用时自动刷新重写规则
手动刷新固定链接可能不彻底,添加激活/停用钩子强制刷新:
// 插件激活时刷新规则 function hob_ap_activate() { hob_post_type(); hob_add_taxonomies(); flush_rewrite_rules(); } register_activation_hook( __FILE__, 'hob_ap_activate' ); // 插件停用时刷新规则,清理旧规则 function hob_ap_deactivate() { flush_rewrite_rules(); } register_deactivation_hook( __FILE__, 'hob_ap_deactivate' );
4. 确认public参数配置完整
确保自定义文章类型可被前端查询:
'public' => true, 'publicly_queryable' => true, // 显式设置,继承public值但显式声明更稳妥 'exclude_from_search' => false, // 可选,若需让自定义文章能被搜索到
内容的提问来源于stack exchange,提问作者Lucart06
相关产品推荐
相关产品推荐

