使用REGISTER_POST_TYPE创建自定义页面

时间:2013-04-08 作者:Lucas Bustamante

函数中的以下代码。php在我发布时输出帖子,而不是页面:

// custom post_type for agenda
add_action( \'init\', \'create_my_post_types\' );    
function create_my_post_types() {
    register_post_type( \'agenda\', array(
        \'labels\' => array(
            \'name_admin_bar\' => _x( \'Agenda\', \'add new on admin bar\' ),
        ),
        \'public\' => true,
        \'publicly_queryable\' => false,
        \'capability_type\' => \'page\',
        \'map_meta_cap\' => true,
        \'hierarchical\' => true,
        \'rewrite\' => false,
        \'query_var\' => false,
        \'delete_with_user\' => true,
        \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'page-attributes\', \'custom-fields\', \'comments\', \'revisions\' ),
    ) );}
我做错了什么?

完整问题:

我需要在页面中添加一个元框,但只需;“议程”;page,我不希望此元框出现在管理面板的每个页面中,因此我正在创建一个自定义post\\u类型,其功能与页面完全相同,只有使用不同的post\\u类型才能单独为其设置元框。

电流输出屏幕截图:

http://i.stack.imgur.com/oPEx8.png(尚无法发布直接图像,请单击链接)

2 个回复
SO网友:tarfish

您的屏幕截图似乎表明您对自定义帖子类型的状态消息感兴趣\'Page updated. View page\'. 如果这是真的,你得到\'Post updated. View post\' 您没有为自定义帖子类型提供状态消息。

下面是管理编辑页面的代码(edit-form-advanced.php):

if ( isset($_GET[\'message\']) ) {
    $_GET[\'message\'] = absint( $_GET[\'message\'] );
    if ( isset($messages[$post_type][$_GET[\'message\']]) )
        $message = $messages[$post_type][$_GET[\'message\']];
    elseif ( !isset($messages[$post_type]) && isset($messages[\'post\'][$_GET[\'message\']]) )
        $message = $messages[\'post\'][$_GET[\'message\']];
}
因此,编辑屏幕正在尝试为您的$post_type (议程),未找到它们,并显示帖子。

您可以通过创建$messages[\'agenda\'] 使用类似于Register Post Type function reference:

function agenda_update_messages( $messages ) {
  global $post, $post_ID;
  $messages[\'agenda\'] = array(
    // ...
    1 => sprintf( __(\'Page updated. <a href="%s">View page</a>\', \'your_text_domain\'),
                  esc_url( get_permalink($post_ID) ) ),
    // ...
  ); 
  return $messages;
}
add_filter( \'post_updated_messages\', \'agenda_update_messages\' );

SO网友:Bainternet

如果页面是您需要的页面,则无需创建自定义帖子类型,只需添加元框即可,例如:

add_action(\'admin_init\',\'meta_on_agenda_only\');

function meta_on_agenda_only(){
    $post_id = $_GET[\'post\'] ? $_GET[\'post\'] : $_POST[\'post_ID\'] ;

    // checks for post/page ID change the 00 to the ID pf your agenda page
    if ($post_id == \'00\' || is_page(\'agenda\'))  {
        //add your metabox
        add_meta_box(\'meta_box_id\', \'Meta Box name\', \'callback_function\', \'page\', \'normal\', \'high\');
    }
    //hook the save action
    add_action(\'save_post\',\'my_meta_save\');

}

结束

相关推荐

Custom metabox not working

早晨我有个电话functions.php 在我的主题目录中包含一个自定义元框php文件,名为:/metaboxes/home-meta.php调用代码如下: add_action( \'add_meta_boxes_page\',\'load_home_meta\' ); function load_home_meta() { $post_id = $_GET[\'post\'] ? $_GET[\'post\'] : $_POST[\'post_ID\']