• Hi everyone. This is my first post in this support forum.

    My page is using the theme “Kale Child”.

    Context
    Under the ‘Blog’ menu items (which I have renamed to ‘Categories’) have roughly 20 different posts divided into 6 categories. Some categories have 6-7 blog posts at the moment, with more to come. I need each category to always show all blog posts under that category.

    I know I can achieve this by setting the ‘Blog pages show at most’ setting (Settings > Reading) to – say – 100. However, that has the unfortunate side effect of then cluttering my home page which would then show all blog posts. And I want the home page to only show the latest 4.

    My ask
    I believe it should be possible to write some CSS which would allow me to only show the latest 4 posts on the front page even if the ‘Blog pages show at most’ setting is set to 100. Would someone be able to help me write that?

    • This topic was modified 3 years, 11 months ago by kidznomz.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Hi @kidznomz,

    Nice looking site! Thanks for using Kale, and thank you for getting in touch.

    You could use CSS to hide the rest of the posts, but a more efficient solution might be to prevent them from being loaded in the first place. It looks like you’re using a child theme, so you can just copy + paste the following code into your child theme’s functions.php file:

    add_action('pre_get_posts', function($query) {
    	if (!($query->is_home() && is_main_query()) || is_admin()) {
    		return;
    	}
    	if ($query->is_paged) {
    		$offset = (($query->query_vars['paged'] - 2) * get_option('posts_per_page')) + 4;
    		$query->set('offset', $offset);
    	} else {
    		//$query->set('posts_per_page', $offset + $ppp);
    		$query->set('posts_per_page', 4);
    	}
    });
    add_filter('found_posts', function($found, $query) {
    	if ($query->is_paged) {
    		$found += (get_option('posts_per_page') - 4);
    	}
    	return $found;
    }, 10, 2);

    If your functions.php has a closing PHP tag at the end of the file (it will look like this: ?>), then you can insert the code right before that line. If there’s no closing PHP tag, you can just insert the code at the end of the file.

    Let us know if you have any trouble or any additional questions!

Viewing 1 replies (of 1 total)
  • The topic ‘Override number of blog posts on home page’ is closed to new replies.