When I compose a site or blog by adapting to WordPress, I have come up with a great idea in my head, but I often encounter reality that lacks the coding ability to realize it.
Today, I had the idea to set the number of articles to be displayed per page, which is set to 10 by default, on the main page and category or archive page of the site. I used the coding for this, . It is very useful.
WordPress site main page 'index.php' only change the number of posts per page
If you search for ways to change the number of posts per page, there is not much data available, but sometimes the information you see is too complicated and inefficient.
So I wanted to make the simplest modification, so what I found was the WordPress API itself. In general, you can use the code provided by WordPress directly with fuction.php to get the most stable, light, and easily desired effect.
soWordPress Official DocumentationI found the code I wanted in my code.
function hwl_home_pagesize( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( is_home() ) { // Display only 1 post for the original blog archive $query->set( 'posts_per_page', 1 ); return; } if ( is_post_type_archive( 'movie' ) ) { // Display 50 posts for a custom post type called 'movie' $query->set( 'posts_per_page', 50 ); return; } } add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );
To explain the code, the first paragraph means start the article-related function, the second red
Red painted number
Painted third blue
that
If you want to add an archive you want to set up, you can just continue to create the third piece of code, like this
function hwl_home_pagesize( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( is_home() ) { // Display only 1 post for the original blog archive $query->set( 'posts_per_page', 1 ); return; } if ( is_post_type_archive( 'movie' ) ) { // Display 50 posts for a custom post type called 'movie' $query->set( 'posts_per_page', 50 ); return; } if ( is_post_type_archive( 'picture' ) ) { // Display 50 posts for a custom post type called 'movie' $query->set( 'posts_per_page', 50 ); return; } } add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );
And if you only need to control the number of posts on the main page like GKKmon, you can erase all the useless parts and use only the following code.
function hwl_home_pagesize( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( is_home() ) { // Display only 1 post for the original blog archive $query->set( 'posts_per_page', 1 ); return; } } add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );
Why and how to control the number of posts on the main page
Usually, you can control the number of articles on the main page when you want to expose various elements besides posting on the main page, or when you want to decorate the index page in magazine format.
Currently, the main page of GKKmon.com is also used to expose functional tools at the top, then two new posts, and then sort the magazines and information media appropriately by listing the documents by category.
In this way, you can freely make use of the main page according to your taste.