为了解决这个问题,我浏览了四到五个Codex页面、几十个Stackexchange页面和四到五个开发人员博客。
我有一个自定义的帖子类型,叫做authors。CPT设置正确,我从Codex中提取了CPT框架,并对照CPT生成器进行了检查。管理屏幕正确创建、编辑和列出作者帖子,URL格式正确example.com/author/post_name/
. 作者档案也起作用。
但我所做的一切都不会在前端显示该页面。只不过是404。
我已经在Permalinks页面和WP-CLI中刷新了十几次重写。我还安装了定制的Post类型Permalinks,并且有相同的问题。
CPT在这里:
add_action( \'init\', \'to4_cpt_author\' );
function to4_cpt_author() {
$labels = array(
\'name\' => _x( \'Authors\', \'post type general name\' ),
\'singular_name\' => _x( \'Author\', \'post type singular name\' ),
\'menu_name\' => _x( \'Authors\', \'admin menu\' ),
\'name_admin_bar\' => _x( \'Author\', \'add new on admin bar\' ),
\'add_new\' => _x( \'Add New\', \'author\' ),
\'add_new_item\' => __( \'Add New Author\' ),
\'new_item\' => __( \'New Author\' ),
\'edit_item\' => __( \'Edit Author\' ),
\'view_item\' => __( \'View Author\' ),
\'all_items\' => __( \'All Authors\' ),
\'search_items\' => __( \'Search Authors\' ),
\'parent_item_colon\' => __( \'Parent Authors:\' ),
\'not_found\' => __( \'No authors found.\' ),
\'not_found_in_trash\' => __( \'No authors found in Trash.\' )
);
$args = array(
\'labels\' => $labels,
\'description\' => __( \'Author post type.\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => \'author\',
\'rewrite\' => array(\'slug\' => \'author\', \'with_front\' => true ),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => 9,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ),
\'taxonomies\' => array( \'admin_tag\', \'post_tag\' ),
\'menu_icon\' => \'dashicons-id-alt\'
);
register_post_type( \'author\', $args );
}
我已经安装了查询监视器插件,它告诉我以下重写是匹配的。
All Matching Rewrite Rules
Rule Query
([^/]*)/([^/]*)/?$ post_type=post
&name=$matches[2]
&meta=$matches[1]
^author/([^/]*)/? post_type=author
&name=$matches[1]
author/([^/]+)(?:/([0-9]+))?/?$ author=$matches[1]
&page=$matches[2]
(.?.+?)(?:/([0-9]+))?/?$ pagename=$matches[1]
&page=$matches[2]
第二个查询
^author/([^/]*)/?
是我用以下方法编写的:
function to4_rewrite_rule() {
add_rewrite_rule( \'^author/([^/]*)/?\', \'index.php?post_type=author&name=$matches[1]\',\'top\' );
}
add_action(\'init\', \'to4_rewrite_rule\', 10, 0);
但最上面的查询似乎首先得到匹配,这就是为什么我得到404。查询监视器将生成的查询显示为
name=catherine-collins&post_type=post
应该是什么时候
name=catherine-collins
&post_type=author
该查询来自何处,如何降级?没有别的了add_rewrite_rule
在我的主题或任何插件的任何地方,所以我猜这是核心。主题是217,我自己的自定义插件定义了几种自定义帖子类型。