Menu Close

How to adjust the number of articles displayed by WordPress main index page and archives

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'is_home'Is the part of the main page, index.php, that controls the number of posts per page.

Red painted number'1'If you adjust the number of articles to be displayed on the main page, and you enter them as example code, you will see only one article on the main page. On the other pages, it will be displayed as set in 'WordPress Admin Page'> 'Settings'> 'Read'> 'Number of posts per page'.

Painted third blue'movie'Section defines the number of posts to display on the archive page.

that'movie'Enter the name of the archive to be applied to the part, and then set the number in the blue '50' to the number of posts in the archive.

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.

Related posts - 관련 글

Comment (Information not required)

Posted in All, Wordpress

이메일 구독 - Email Subs

최선을 다해 직접 만든 콘텐츠만 공유합니다.
We share the best content we have created.