Archived topic
How to display post category archive as home page
8 replies · Started by Deep on April 23, 2019
Hi,
I have a query on how to make a post category archive display as homepage? I don't like to use any plugin in this regard.
I found one support ticket in this regard https://generatepress.com/forums/topic/custom-post-type-category-archive-as-a-homepage/
but seems to quite confusing on my part.
Please help.
Thanks.
Hi there,
that user was requiring something more complicated as it was for a Custom Post Type.
Are you wanting to display the normal Posts?
Hi,
I simply want a specific category post archive to be displayed in the home page (instead of all categories of posts). Like I want to display only 'News' category articles on the homepage.
Got it. Thanks.
The simplest way would to be to use a Plugin. WP Show Posts is perfect for adding post grids to a static page which means you can add other content to the page. It's one of Tom's plugins so is super lightweight:
See I told you earlier that I would not use any plugin for this. They will be putting some extra burden on my resources. And I found Mr. Tom was guiding with the code for the same.
So, I would appreciate if you can help me out of the situation through PHP codes.
Thanks.
Not sure if it will work, but you could try this:
add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '123' );
}
} );
You'd just need to:
1. Set your front page to display posts.
2. Update 123 to the ID of the category you want to display.
You are superb. your code was working nice. I am quite impressed with your support.
But, I simply made a small change in your code to offset the latest post
add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '252' );
$query->set( 'offset', '1' );
}
} );
The problem is I could offset the latest post from the page but when I go down and click load more button, it loads the same set of posts once again. What to do? Thanks.
You're welcome :)
I am opening this ticket as I have mentioned in my earlier post #877750 that I have a little problem with offset and pagination. Please have a look.