Php 如何将页面附加到自定义帖子类型
这在过去对我有效,所以我不确定为什么它现在不起作用 我创建了一个自定义帖子类型:Php 如何将页面附加到自定义帖子类型,php,wordpress,function,custom-post-type,Php,Wordpress,Function,Custom Post Type,这在过去对我有效,所以我不确定为什么它现在不起作用 我创建了一个自定义帖子类型: add_action('init', 'register_team'); function register_team(){ $args = array( 'label' => __('Design Team'), 'singular_label' => __('Design Team'), 'public'
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的自定义post类型,并通过创建一个名为page-showroom.php的文件将该自定义post类型附加到一个页面。但是,当我创建一个名为page-team.php的文件时,它将不会与此页面关联。这是语法问题吗
更新
我通过在CMS中创建一个页面,并使用页面属性添加模板来解决这个问题。我不特别喜欢这个解决方案的原因是,用户可能会更改页面的模板,导致它不再工作
我只是觉得我缺少了一些关于WP核心如何定义页面-??变量模板名称或它是一个打字错误,愚蠢的错误,等等
更新
根据要求,下面是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(); ?>
您需要添加add_action('init','register_team')代码>在函数之前。我通常只遵循模板层次结构。在您的情况下——我假设您希望页面列出post type=team的所有帖子——这意味着在“page templates”目录下创建一个名为“archive team.php”的页面。或者,如果您只想显示单个帖子,可以使用“single team.php”。至少你应该这样做。我这样做,对我来说很有效
这是我的功能顶部包含的。如果该行不存在,它将不会填充到CMS中。我认为这是一个命名问题。为自定义帖子类型创建循环文件,并创建一个单独的文件来显示单个自定义帖子类型的内容。您是否有过支持页面模板的CPT?我的意思是,在属性元框中显示了模板选择?这是不可能的。无论如何,您应该将'page-attributes'
添加到'supports'
键中,尽管这只会在属性元框中显示父项和顺序。附言:好问题!我确实创建了一个名为showrooms的CPT(完全相同的方式),然后创建了一个名为page-showrooms.php的页面,该模板被附加到CPT上,而无需在页面属性框下应用它,因此这似乎是可能的。可能是侥幸或错误,但也有可能。对,那么页面模板的工作方式不一样吗?我成功地为单个艺术家创建了一个模板,效果很好,就像页面展示室一样。团队,这是命名的问题吗?我知道有些词你应该避开,但我不认为“团队”是一个词。。。我需要看更多的代码才能更有帮助。如果可能,您可以发布single-artists.php、single-team.php的完整代码以及这两种自定义发布类型的代码吗?我很确定“团队”不是一个保留的词。我想这是密码里的东西。