我找到了解决办法!要将帖子显示为页面,我需要执行以下步骤:
1) 注册帖子类型
private function init(){
add_action( \'init\', array( $this, \'create_post_type\') );
}
public function create_post_type() {
register_post_type( \'foo\',
array(
\'labels\' => array(
\'name\' => __( \'Foos\' ),
\'singular_name\' => __( \'Foo\' )
),
\'public\' => true,
\'exclude_from_search\' => true,
\'has_archive\' => true,
\'show_in_menu\' => false,
\'show_in_nav_menus\' => false,
\'show_in_admin_bar\' => false,
\'rewrite\' => true //<-- Important !
)
);
}
2)选择模板
private function init(){
add_action( \'init\', array( $this, \'create_post_type\') );
add_filter( \'template_include\', array( $this, \'foo_page_template\'), 99 );
}
public function foo_page_template( $template ) {
if ( get_post_type() === \'foo\' ) {
$new_template = locate_template( array( \'page.php\' ) );
if ( \'\' != $new_template ) {
return $new_template;
}
}
return $template;
}
3)(可选)添加车身类别
add_action( \'body_class\', array($this , \'add_body_classes\' ));
public function add_body_classes( $classes ) {
// Add custom class
$classes[] = \'page\';
$classes[] = \'page-two-column\';
return $classes;
}
href已由更改
mysiteurl.com/foo/my-post-name 如果你不忘记的话,那就行了
\'rewrite\' => true 当您使用
register_post_type() !