これは過去に私にとってはうまくいったので、なぜ今はうまくいかないのかわかりません。
カスタム投稿タイプを作成しました:
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コアがページを定義する方法に関連して何かが欠けているような気がします-?? 可変テンプレート名またはそれはタイプミス、愚かな間違いなどです...
更新 要求に応じて、これが私のCPTのすべてをロードするfunctions.phpからのコードです
// 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(); ?>