How to use custom post type archive as front page

We can easily set any page as front page from Settings > Reading. It can be any static page. But what if we want to set an archive or CPT archive page as front page?

There are many ways to do so. We can create a custom page template, and write our code to get the posts for the post type (will look like an archive). Then create a wordpress page using that page template and select that page as front page from Settings > Reading.

This is a little complex if you are not a developer. Or even if you use a custom theme. Let’s do it in another way.

What you need is to have a file inside your theme for archive – archive.php. For CPT the file name will be archive-{CPT}.php. We will use the same file to make our home page.

You may also read:  jSlider Plugin for jQuery (for 3 horizontal div)

For example, if your CPT archive url is domain.com/my_cpts then we want to see exact same layout and same content of domain.com/my_cpts in domain.com/.

What I love to do in this case is to use a very simple snippet. Where I just need to set the CPT name, that’s it. Here is the code:


<?php
/*
* 1. Go to Settings > Permalinks and select any page as home page
* Then use the following code
*
* You can add those codes in your functions.php in the theme, if you think your theme won’t be changed.
* Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'.
* If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that,
* give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins,
* so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.
*
* Just change the correct CPT name
*/
add_action( "pre_get_posts", "archive_page_as_front_page" );
function archive_page_as_front_page( $query ){
if( is_admin() ) return;
if( $query->get( 'page_id' ) == get_option( 'page_on_front' ) ){
$query->set( 'post_type', 'SET YOUR CPT NAME' );
$query->set( 'page_id', '' );
$query->is_page = $query->is_singular = ;
$query->is_archive = $query->is_post_type_archive = 1;
}
}
You may also read:  Remove joined group notification from sitewide activity

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

You may also read:  Team Bio – Meet the Team – WordPress Plugin

Note that, here CPT means Custom Post Type

Enjoy!

You may also like...

Leave a Reply

%d bloggers like this: