Skip to content

Instantly share code, notes, and snippets.

@smeyer
Created July 16, 2012 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smeyer/3123298 to your computer and use it in GitHub Desktop.
Save smeyer/3123298 to your computer and use it in GitHub Desktop.
news post type
<?php
function create_news_post_type() {
register_post_type( 'news',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News' ),
'add_new' => __( 'Add News Item' ),
'add_new_item' => __( 'Add News Item' ),
'edit_item' => __( 'Edit News Item' ),
'new_item' => __( 'Add News Item' ),
'view_item' => __( 'View News Item' ),
'search_items' => __( 'Search News' ),
'not_found' => __( 'No News Items found' ),
'not_found_in_trash' => __( 'No news items found in trash' )
),
'public' => true,
'rewrite' => array('slug' => 'news-item'),
'show_in_nav_menus' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'register_meta_box_cb' => 'add_news_metabox'
)
);
}
add_action( 'init', 'create_news_post_type' );
/* Add meta box */
function add_news_metabox() {
add_meta_box('env_feat_news', 'Featured', 'env_feat_news', 'news', 'side', 'high');
}
function env_feat_news($post) {
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="envmeta_noncename" id="envmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the location data if its already been entered
$news_feat = get_post_meta($post->ID, '_home_feat', true);
// Echo out the field
echo '<label for="_home_feat">Feature on homepage?</label> ';
?><input type="checkbox" name="_home_feat" id="news_featured" <?php checked( $news_feat, 'on' ); ?> /><?php
}
/* Save the Metabox Data */
function env_save_feat_news($post_id, $post) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['envmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
$chk = isset( $_POST['_home_feat'] ) && $_POST['_home_feat'] ? 'on' : 'off';
update_post_meta( $post_id, '_home_feat', $chk );
}
add_action('save_post', 'env_save_feat_news', 1, 2); // save the custom fields
/* Add meta to column view */
function news_featured_columns( $columns )
{
$columns = array(
'cb' => '<input type="checkbox" />',
'date' => __( 'Date' ),
'title' => __( 'Title' ),
'_home_feat' => __( 'Featured?' ),
);
return $columns;
}
add_filter( 'manage_edit-news_columns', 'news_featured_columns', 10, 1 );
function my_manage_news_columns( $column, $post_id ) {
global $post;
switch( $column ) {
case '_home_feat' :
/* Get the post meta. */
$feat = get_post_meta( $post_id, '_home_feat', true );
if ($feat == 'on') { echo '<b>yes</b>'; }
else { echo ''; }
break;
/* Just break out of the switch statement for everything else. */
default :
break;
}
}
add_action( 'manage_news_posts_custom_column', 'my_manage_news_columns', 10, 2 );
// Allow thumbnails to be used on events post type
add_image_size( 'news-thumbnail', 120, 100, false );
add_image_size( 'news-med', 260, 200, false );
add_image_size( 'news-full', 9999, 9999, false );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment