Custom post creation:
/**
* Custom Post Type: Stories
*
*/
add_action('init', 'rr_story_register', 0); // Register custom post type
function rr_story_register() {
$labels = array(
'name' => _x('Client Stories', 'post type general name'),
'singular_name' => _x('Story', 'post type singular name'),
'all_items' => 'All Stories',
'add_new' => _x('Add New', 'story'),
'add_new_item' => __('Add New Story'),
'edit_item' => __('Edit Story'),
'new_item' => __('New Story'),
'view_item' => __('View Story'),
'search_items' => __('Search Stories'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( "slug" => "story"),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 9,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions')
);
register_post_type( 'story' , $args );
//flush_rewrite_rules( );
}
and the Front Page display:
if ( ! function_exists( 'display_random_story_post' ) ) :
/**
* Displays latest blog post on the home page.
* First post will show excerpt/phone, rest will just be a list.
*
* @return null
*/
function display_random_story_post() {
// Setup the Query
$args = array (
'post_type' => 'story',
'posts_per_page' => 1,
'orderby' => 'rand'
);
$rand_story = new WP_Query( $args );
// Setup var
$display = null;
//foreach ( $rand_story as $story ) : setup_postdata( $story );
if($rand_story->have_posts()) :
while($rand_story->have_posts()) : $rand_story->the_post();
$client_name = get_post_meta( get_the_ID(), 'rr_story_name', true);
$client_location = get_post_meta( get_the_ID(), 'rr_story_location', true);
$page_headline = get_post_meta( get_the_ID(), 'rr_story_headline', true);
$use_alt_headline = 1;
if ($page_headline == '') {
$use_alt_headline = 0;
$page_headline = get_the_title();
}
$display = '<article id="post-' .get_the_ID(). '" class="latest-post">' . "\r\n";
$display .= '<header>' . "\r\n";
$display .= '<h2><a href="' .get_permalink(). '">' .$page_headline. '</a></h2>' . "\r\n";
if( $use_alt_headline == 1 && $client_location !== '') {
$display .= '<div class="entry-meta"><p>' .$client_name. ', ' .$client_location. '</p></div>';
} elseif( $use_alt_headline == 1 ) {
$display .= '<div class="entry-meta"><p>' .$client_name. '</p></div>';
} else {
$display .= '<div class="entry-meta"><p>' .$client_location. '</p></div>';
}
$display .= '</header>' . "\r\n";
$display .= return_excerpt(280);
$display .= '</article><!-- END #post-' .get_the_ID(). ' -->' . "\r\n";
endwhile; // End post display loop
else: // If there are no posts, say so
$display .= '<p>We're sorry, there are no stories to display.</p>';
endif; // End loop to get blog posts
echo $display;
wp_reset_postdata();
} // END display_random_story_post()
endif;