所需行为:
自定义帖子类型slug:研究人员、学生、合作者
URL:http://domainexample.com/people/researchers, http://domainexample.com/people/students, http://domainexample.com/people/collaborators<模板文件:存档人员。php
如何设置自定义帖子类型以实现此行为
我想在不使用分类法的情况下完成它
可能吗?
试图设置has_archive
slug和rewrite
缓动至people/researchers
, 虽然加载的模板文件仍为arquive,但仍会加载帖子。php。
当前“研究人员”自定义帖子类型注册码:
$args = array(
"label" => __( "Researchers", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"has_archive" => "people/researchers",
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "people/researchers", "with_front" => true ),
"query_var" => true,
"supports" => array( "title" ),
);
register_post_type("researchers", $args);
谢谢你
最合适的回答,由SO网友:Milo 整理而成
您可以在rewrite
slug
和has_archive
参数:
$args = array(
\'rewrite\' => array(\'slug\' => \'people/researchers\'),
\'has_archive\' => \'people/researchers\',
// other args...
);
EDIT
你的post type slug是
researchers
, WordPress将查找该文件
archive-researchers.php
默认情况下。如果要强制使用其他模板,可以使用
archive_template
过滤器:
function wpd_researchers_archive_template( $archive_template ){
if( is_post_type_archive( \'researchers\' ) ){
$archive_template = locate_template( \'archive-people.php\' );
}
return $archive_template;
}
add_filter( \'archive_template\', \'wpd_researchers_archive_template\' );