0
/* ---------------------------------------------- */

/* Setting Post Limits on the HomePage */

/* ---------------------------------------------- */

if ( ! function_exists( 'the_post_limit' ) ) {

    if ( true == get_theme_mod( 'the_post_limit', true ) ) {

        function the_post_limit( $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', 8 );

                return;

            }

        }

        add_action( 'pre_get_posts', 'the_post_limit', 1 );

    }

}

The above code is sitting in the theme's functions.php. The purpose here is to restrict the number of posts on the home page.

The current number here is the number "8".

Can we somehow produce the number directly from the settings? I mean whatever number we put there. enter image description here

1
  • 1
    But you don’t need any filter then... What am I missing here? Aug 7, 2018 at 5:11

1 Answer 1

1

I modified your code just a bit. See if this works

 if ( ! function_exists( 'the_post_limit' ) ) {    
        if ( true == get_theme_mod( 'the_post_limit', true ) ) {    
            function the_post_limit( $query ) {   
                if ( is_admin() || ! $query->is_main_query() )    
                    return;    
                if ( is_home() ) {    
                    // Display only 1 post for the original blog archive
                       $get_default_posts_per_page = get_option( 'posts_per_page' );
                    $query->set( 'posts_per_page', $get_default_posts_per_page );    
                    return;    
                }    
            }    
            add_action( 'pre_get_posts', 'the_post_limit', 1 );

        }

    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.