Custom Post Type setup

时间:2018-01-28 作者:Diogo Bento

所需行为:

自定义帖子类型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);
谢谢你

1 个回复
最合适的回答,由SO网友:Milo 整理而成

您可以在rewrite slughas_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\' );

结束

相关推荐

以REST API结果形式表示的WP_QUERY结果

这是一个简单的posts结果数组。$query = new WP_Query( array( \'post_type\' => \'post\' ) ); $posts = $query->posts; // returns simple array of data 有没有办法从wp-json/wp/v2/posts/获取结果_嵌入而不向服务器发出额外请求以提取json,然后解码到php数组?寻找类似的东西:$posts = $query->rest_posts();