I just set up a custom post type, mostly following this tutorial.
I'm setting up things on my site now, doing a lot of CSS modifications and such, and every few minutes the post just forgets everything that's in the custom meta boxes.
I'm sure I've done something wrong in the add_action/update_post_meta section, but I don't know what it is, because I don't really understand the entirety of what the code does.
In any case, here's the entire thing:
add_action('init', 'portfolio_register');
function portfolio_register() {
$labels = array(
'name' => _x('My Portfolio', 'post type general name'),
'singular_name' => _x('Portfolio Item', 'post type singular name'),
'add_new' => _x('Add New', 'portfolio item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'portfolio' , $args );
}
register_taxonomy("portfolio_cat", array("portfolio"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true));
add_action("admin_init", "admin_init");
function admin_init(){
add_meta_box("media_meta", "Media", "media_meta", "portfolio", "normal", "low");
add_meta_box("highlights_meta", "Highlights", "highlights_meta", "portfolio", "normal", "low");
add_meta_box("main_meta", "Main", "main_meta", "portfolio", "normal", "low");
add_meta_box("tabbed_meta", "Tabbed", "tabbed_meta", "portfolio", "normal", "low");
}
function media_meta(){
global $post;
$custom = get_post_custom($post->ID);
$media_meta = $custom["media_meta"][0];
?>
<p><label>Media:</label><br />
<textarea cols="110" rows="5" name="media_meta"><?php echo $media_meta; ?></textarea></p>
<?php
}
function highlights_meta(){
global $post;
$custom = get_post_custom($post->ID);
$highlights_meta = $custom["highlights_meta"][0];
?>
<p><label>Highlights:</label><br />
<textarea cols="110" rows="5" name="highlights_meta"><?php echo $highlights_meta; ?></textarea></p>
<?php
}
function main_meta(){
global $post;
$custom = get_post_custom($post->ID);
$main_meta = $custom["main_meta"][0];
?>
<p><label>Main:</label><br />
<textarea cols="110" rows="5" name="main_meta"><?php echo $main_meta; ?></textarea></p>
<?php
}
function tabbed_meta(){
global $post;
$custom = get_post_custom($post->ID);
$tabbed_meta = $custom["tabbed_meta"][0];
?>
<p><label>Tabbed:</label><br />
<textarea cols="110" rows="5" name="tabbed_meta"><?php echo $tabbed_meta; ?></textarea></p>
<?php
}
add_action('save_post', 'save_details');
function save_details(){
global $post;
update_post_meta($post->ID, "media_meta", $_POST["media_meta"]);
update_post_meta($post->ID, "highlights_meta", $_POST["highlights_meta"]);
update_post_meta($post->ID, "main_meta", $_POST["main_meta"]);
update_post_meta($post->ID, "tabbed_meta", $_POST["tabbed_meta"]);
}
add_action("manage_posts_custom_column", "portfolio_custom_columns");
add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
function portfolio_edit_columns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Portfolio Title",
"description" => "Description",
"media_meta" => "Media",
"highlights_meta" => "Highlights",
"main_meta" => "Main",
"tabbed_meta" => "Tabbed",
"portfolio_cat" => "Portfolio Categories"
);
return $columns;
}
function portfolio_custom_columns($column){
global $post;
switch ($column) {
case "description":
the_excerpt();
break;
case "media_meta":
$custom = get_post_custom();
echo $custom["media_meta"][0];
break;
case "highlights_meta":
$custom = get_post_custom();
echo $custom["highlights_meta"][0];
break;
case "main_meta":
$custom = get_post_custom();
echo $custom["main_meta"][0];
break;
case "tabbed_meta":
$custom = get_post_custom();
echo $custom["tabbed_meta"][0];
break;
case "portfolio_cat":
echo get_the_term_list($post->ID, 'portfolio_cat', '', ', ','');
break;
}
}
admin_init()have a REALLY high possibility of conflicting with other core or plugin functions. – m0r7if3r Feb 14 '12 at 2:05