Displaying custom post types on your WordPress blog homepage

So you have created a great new custom post type in WordPress 3.0 but it doesn’t show on your homepage. To add your new post type to the homepage you could use the following code in your functions.php. As you can see this code sets all the post types you would like to have displayed by using the pre_get_posts filter. I think using the filter would be great but currently it breaks the new menu’s in WordPress 3.0.

add_filter( 'pre_get_posts', 'my_get_posts' );
 
function my_get_posts( $query ) {
	if ( is_home() )
		$query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote', 'attachment' ) );
 
	return $query;
}

So to add your custom post types into the your blog homepage you can put this code just before the WordPress post loop. The following code adds in the post types you would like to use and also checks to make sure your paging still works.

<?php 
if( is_home() ){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array('post_type'=>array( 'post', 'linkpost'),'paged'=>$paged ) );
}
?>

if you want to read more about the first piece of code you can check it out on Justin Tadlock’s blog.

Related Posts

  1. Harness the Power of the WordPress Post Class Function and Style Those Sticky Posts
  2. How to see if a Post in WordPress has an Excerpt
  3. Awesome WordPress Feature of the Day: Add Theme Support
  4. Adding Twitter to Your Web Site with JavaScript
  5. Function Override for AS3?
This entry was posted in WordPress. Bookmark the permalink.

2 Responses to Displaying custom post types on your WordPress blog homepage

  1. thanks Josh, that worked like a charm!
    now, how can I get custom post types into a widget? any ideas because I am at a dead end:)
    -eric

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">