这在过去对我有用,所以我不确定为什么它现在不起作用。
我创建了一个自定义帖子类型:
add_action('init', 'register_team');
function register_team(){
$args = array(
'label' => __('Design Team'),
'singular_label' => __('Design Team'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "design-team",'with_front' => true), // Permalinks format
'supports' => array( 'title', 'editor', 'thumbnail' ),
'add_new' => __( 'Add New Member' ),
'add_new_item' => __( 'Add New Member' ),
'edit' => __( 'Edit Member' ),
'edit_item' => __( 'Edit Member' ),
'new_item' => __( 'New Member' ),
'view' => __( 'View Member' ),
'view_item' => __( 'View Member' ),
'search_items' => __( 'Search Design Team' ),
'not_found' => __( 'No info found' ),
'not_found_in_trash' => __( 'No info found in Trash' ),
'parent' => __( 'Parent Info' ),
'menu_position' =>__( 7 ),
);
register_post_type( 'team' , $args );
}
并调用了我可以在 CMS 中看到的功能,添加新条目等。我需要将页面模板附加到此自定义帖子类型。在同一个站点上,我创建了一个名为 showroom 的自定义帖子类型,并通过创建一个名为 page-showroom.php 的文件将自定义帖子类型附加到页面。但是,当我创建一个名为 page-team.php 的文件时,它不会与该页面关联。这是语法问题吗?
更新 我通过在 CMS 中创建一个页面并使用页面属性添加模板来解决这个问题。我不特别喜欢这个解决方案的原因是用户可能会更改页面的模板,导致它不再工作。
我只是觉得我错过了一些与 WP Core 如何定义页面相关的东西-?? 变量模板名称或者它是一个错字,愚蠢的错误,等等......
更新 按要求,这是来自functions.php的代码,它加载了我所有的CPT
// CUSTOM POST TYPES
add_action('init', 'register_showroom');
add_action('init', 'register_project_gallery');
add_action('init', 'register_slideshow');
add_action('init', 'register_team');
// ADD Showroom
function register_showroom(){
$args = array(
'label' => __('Showroom'),
'singular_label' => __('Showroom'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "showroom",'with_front' => true), // Permalinks format
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'page-attributes' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New' ),
'view' => __( 'View' ),
'view_item' => __( 'View' ),
'search_items' => __( 'Search Showroom' ),
'not_found' => __( 'No info found' ),
'not_found_in_trash' => __( 'No info found in Trash' ),
'parent' => __( 'Parent Info' ),
'menu_position' =>__( 4 ),
);
register_post_type( 'showroom' , $args );
}
// ADD Project Gallery
function register_project_gallery(){
$args = array(
'label' => __('Project Gallery'),
'singular_label' => __('Project Gallery'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "project-gallery",'with_front' => true), // Permalinks format
'supports' => array( 'title', 'editor', 'thumbnail' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New' ),
'view' => __( 'View' ),
'view_item' => __( 'View' ),
'search_items' => __( 'Search Project Gallery' ),
'not_found' => __( 'No info found' ),
'not_found_in_trash' => __( 'No info found in Trash' ),
'parent' => __( 'Parent Info' ),
'menu_position' =>__( 5 ),
);
register_post_type( 'project_gallery' , $args );
}
// ADD Slideshow
function register_slideshow(){
$args = array(
'label' => __('Homepage Slideshow'),
'singular_label' => __('Homepage Slideshow'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "project-gallery",'with_front' => true), // Permalinks format
'supports' => array( 'title', 'excerpt', 'thumbnail' ),
'add_new' => __( 'Add New Slide' ),
'add_new_item' => __( 'Add New Slide' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New' ),
'view' => __( 'View' ),
'view_item' => __( 'View' ),
'search_items' => __( 'Search Homepage Slideshow' ),
'not_found' => __( 'No info found' ),
'not_found_in_trash' => __( 'No info found in Trash' ),
'parent' => __( 'Parent Info' ),
'menu_position' =>__( 6 ),
);
register_post_type( 'slideshow' , $args );
}
// ADD Design Team
function register_team(){
$args = array(
'label' => __('Design Team'),
'singular_label' => __('Design Team'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array("slug" => "design-team",'with_front' => true), // Permalinks format
'supports' => array( 'title', 'editor', 'thumbnail' ),
'add_new' => __( 'Add New Member' ),
'add_new_item' => __( 'Add New Member' ),
'edit' => __( 'Edit Member' ),
'edit_item' => __( 'Edit Member' ),
'new_item' => __( 'New Member' ),
'view' => __( 'View Member' ),
'view_item' => __( 'View Member' ),
'search_items' => __( 'Search Design Team' ),
'not_found' => __( 'No info found' ),
'not_found_in_trash' => __( 'No info found in Trash' ),
'parent' => __( 'Parent Info' ),
'menu_position' =>__( 7 ),
);
register_post_type( 'team' , $args );
}
所以我可以成功创建一个page-showroom.php、page-project_gallery.php、single-project_gallery.php、single-showroom.php,它们会自动将自己附加到正确的CPT,但如果我创建page-team.php,它只会加载page.php。
这是 page-showroom.php 的示例,它可以工作:
<?php /* Template Name: Showroom */ ?>
<?php get_header(); ?>
<div id="primary" class="site-content showroom">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'showroom' ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
</div>
<?php get_footer(); ?>
和 page-team.php,它不起作用
<?php /* Template Name: Team */ ?>
<?php get_header(); ?>
<div id="primary" class="site-content team">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'team' ); ?>
<?php //comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
</div>
<?php get_footer(); ?>