Creating and Displaying custom post types

Custom Posts introduced in WordPress 3.0 give you the ability to create more than just blog posts. You can now create separate post types for all your content—photos, recipes, labels from your uncles 8-Track collection—you know, anything you can think of. This involves just two steps: Making a new post type and displaying the new posts.

  1. First we need to tell WordPress about your new post type. Paste this into your functions.php file. If the file already has some code in there, omit the opening and closing PHP tags, if it doesn’t leave them in. If your theme doesn’t yet have a functions.php file, go ahead and make one. Functions.php should only have one set of opening and closing PHP tags.
  2.  
    		<?php
    			add_theme_support( 'menus' );
    			add_action( 'init', 'create_my_post_types' );
    			function create_my_post_types() {
    				register_post_type( 'recipes',
    					array(
    						'labels' => array(
    							'name' => __( 'Recipes' ),
    							'singular_name' => __( 'Recipe' )
    						),
    						'public' => true,
    					)
    				);
    			}
    		?>	 
    	
  3. Second (and last, whoot!) we want to display our new recipes for all the world to see, cook and enjoy. Paste this code anywhere you want to display your favourite recipes (may I recommend sidebar.php):
  4. 	<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'posts_per_page' => 10 ) ); ?>
    	<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    	<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
    
    	<div class="entry-content">
    		<?php the_content(); ?>
    	</div>
    	<?php endwhile; ?>
    	 
    

    Hat-tip to Justin Tadlock for this and many more great WordPress tricks.