Most developers use a custom page template if they need to display a custom post types on the front/home page. This is fairly easy to do using a new WP_QUERY:
$args = array( 'post_type' => 'download', ); $downloads = new WP_Query( $args );
However, WordPress still treats this as a page rather than archive, which can be problematic if you have specific styles or scripts that only load on archives or rely on certain body classes. So I started to experiment with a pre_get_posts function. I found this actually works quite well, even with pagination and infinite scrolling scripts.
Here’s the Gist of it:
I borrowed the basic idea from this post on StackExchange, and then refined the home page detection from this great discussion from wpaustralia.
This worked first go, copy/pasted right in to my functions.php. I’d been looking for this solution for a week or so!
thanks!
Nice snippet thx !
Unfortunatly, the pagination works only if you set the home page as a static page. The “if ( ( is_home() && empty( $wp->query_string ) ) ) {” should return nothing instead and let the blog posts be displayed normally.
oops, sorry. This does not work at all with pagination, either the home page is the blog posts index or a static page…
Hi! Thanks for this snippet.
To make the navigation work, I had to edit line 18: instead of
if ( ( is_home() && empty( $wp->query_string ) ) ) {
write :
if ( ( is_home() || is_front_page() && empty( $wp->query_string ) ) ) {
:)
Happy to help!