如何将页面附加到自定义帖子类型

时间:2013-03-13 01:44:01

标签: php wordpress function custom-post-type

这对我来说过去很有用,所以我不确定为什么它现在不起作用。

我创建了一个自定义帖子类型:

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(); ?>

2 个答案:

答案 0 :(得分:1)

您需要在功能之前添加add_action('init', 'register_team');

答案 1 :(得分:1)

我通常只是遵循模板层次结构。在你的情况下 - 我假设你希望你的页面列出post type = team的所有帖子 - 这意味着在你的“page-templates”目录下创建一个名为“archive-team.php”的页面。或者,如果您只想显示单个帖子,则可以使用“single-team.php”。这是你应该这样做的方式,至少。我是这样做的,它对我有用。

http://codex.wordpress.org/Template_Hierarchy