By default, only posts are displayed on your posts page which can either be your home page or blog page depending on your Reading Settings.
If your theme includes custom post types (CPT), you can also add the single pages assigned to your CPT to your posts page as well.
Simply add this PHP code to the end of your child themes functions.php file and change the name portfolio to the name of your CPT.
add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' );
function add_custom_post_types_to_loop( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'portfolio' ) );
return $query;
}
You can add multiple CPT’s to this array like this.
add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' );
function add_custom_post_types_to_loop( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'portfolio', 'video' ) );
return $query;
}
Any custom taxonomy types you add to your CPT will automatically be included in your loop so you don’t need to add them in the array.
You can also add static pages to your posts page if you really want to simply by adding ‘page’ to the array in the above code snippets.
Add Custom Post Type to Archive Page
Change the is_archive() conditional tag to any other like is_category().
add_action( 'pre_get_posts', 'add_custom_post_types_archive' );
function add_custom_post_types_archive( $query ) {
if ( $query->is_main_query() && !is_admin() && is_archive() )
$query->set( 'post_type', array( 'post', 'your-cpt' ) );
return $query;
}
Simply change your-cpt to the name of your custom post type.
Leave a Reply
You must be logged in to post a comment.