已创建自定义帖子类型,但所选模板未保持选中状态

时间:2011-04-20 作者:mrtsherman

我注册了一个名为Resources的新帖子类型。我将“页面属性”指定为受支持的功能。当我去创建一个新的资源帖子时,我会在模板下看到,我可以从我定义的许多自定义模板中选择。但在我选择此选项并单击“更新”后,设置不会保存,模板也不会使用。有什么想法吗?

function create_post_type() {
register_post_type( \'foo_resources\',
    array(
        \'labels\' => array(
            \'name\' => __( \'Resources\' ),
            \'singular_name\' => __( \'Resource\' )
        ),
    \'public\' => true,
    \'has_archive\' => true,
    \'hierarchical\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'menu_position\' => 5,   
    \'supports\' => array(\'title\',\'editor\',\'author\',\'page-attributes\'),
    \'rewrite\' => array(
        \'slug\' => \'resources\',
        \'with_front\' => false
    ),      
    )
);
}

1 个回复
SO网友:Ján Bočínec

自定义帖子类型的页面模板未保存,因为它正在检查帖子类型是否为“页面”-尚未为自定义帖子类型实现该模板。页面模板的下拉列表在3.1中被完全删除。

这是我的处理方法:

/* Define the custom box */

// WP 3.0+
add_action(\'add_meta_boxes\', \'custom_template_add_custom_box\');

// backwards compatible
//add_action(\'admin_init\', \'custom_templatet_add_custom_box\', 1);

/* Do something with the data entered */
add_action(\'save_post\', \'custom_template_save_postdata\');

/* Adds a box to the main column on the Post and Page edit screens */
function custom_template_add_custom_box() {
    add_meta_box( \'custom-post-type-template\', \'Custom Post Type Template\', \'custom_template_inner_box\', \'foo_resources\', \'side\' );
}

/* Prints the box content */
function custom_template_inner_box( $post ) {

    // Use nonce for verification
    wp_nonce_field( plugin_basename(__FILE__), \'custom_template_noncename\' );

    if ( 0 != count( get_page_templates() ) ) {
        $page_template = get_post_meta($post->ID, \'_wp_page_template\', TRUE); ?>
    <p><strong><?php _e(\'Template\') ?></strong></p>
    <label class="screen-reader-text" for="page_template"><?php _e(\'Page Template\') ?></label><select name="page_template" id="page_template">
    <option value=\'default\'><?php _e(\'Default Template\'); ?></option>
    <?php page_template_dropdown($page_template); ?>
    </select>
<?php }
}

/* When the post is saved, saves our custom data */
function custom_template_save_postdata( $post_id ) {

     // verify this came from the our screen and with proper authorization,
     // because save_post can be triggered at other times

     if ( !isset( $_POST[\'custom_template_noncename\'] ) || !wp_verify_nonce( $_POST[\'custom_template_noncename\'], plugin_basename(__FILE__) ) )
         return $post_id;

     // verify if this is an auto save routine. 
     // If it is our form has not been submitted, so we dont want to do anything
     if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) 
         return $post_id;


     // Check permissions
     if ( \'page\' == $_POST[\'post_type\'] ) 
     {
       if ( !current_user_can( \'edit_page\', $post_id ) )
           return $post_id;
     }
     else
     {
       if ( !current_user_can( \'edit_post\', $post_id ) )
           return $post_id;
     }

     // OK, we\'re authenticated: we need to find and save the data

     $page_template = $_POST[\'page_template\'];

    if ( !empty($page_template) && \'foo_resources\' == get_post_type($post_id) ) {
        $page_templates = get_page_templates();
        if ( \'default\' != $page_template && !in_array($page_template, $page_templates) ) {
            if ( $wp_error )
                return new WP_Error(\'invalid_page_template\', __(\'The page template is invalid.\'));
            else
                return 0;
        }
        update_post_meta($post_id, \'_wp_page_template\',  $page_template);
    }
}

/* Custom template redirection hook */

function custom_template_redirect() {
    global $post;

    if ( is_single() && \'foo_resources\' == get_post_type($post) ) {
        $page_template = get_page_template();
        include($page_template);
        exit;
    }
}
add_action("template_redirect", \'custom_template_redirect\');
也许你应该考虑使用Post Formats 作为另一种选择。

结束

相关推荐