Site logo

Archived topic

Limit Parent Category Archive to 10 excerpts

9 replies · Started by Anne on December 9, 2021

Viewing posts 1–10 of 10

I have a category on my site called Tutorials. Underneath it, are various sub-categories, Excel, Google, etc.

Tutorials
- Excel
- Google
- Email
- Etc

Instead of showing all the tutorials on the Tutorials archive page, is there a way to limit it to just 10? The subcategory pages would show the full amount. I've got a custom field called "Priority" that I use for sorting. This way, I could end up with my parent Tutorials page being a "Top 10 Tutorials" page.

Thanks in advance.

Hi Anne,

Try this PHP snippet:

add_filter('pre_get_posts', 'posts_in_category');

function posts_in_category($query){
    if ($query->is_category()) {
        if (is_category('Tutorials')) {
            $query->set('posts_per_archive_page', 10);
        } 
    }
}

Let me know :)

Hi Ying,

thanks for the prompt suggestion. Your code seems to make sense to me so I think we're close although I'm not a coder.

I pasted your code to the Code Snippets plugin. I use it instead of adding code directly to functions.php. However, after adding, I'm still getting more than 10 items under Tutorials.

I did change to lower case "tutorials" as that is the slug name. Upper case didn't work either. And I cleared the cache. I also verified the sub-categories didn't stop at 10.

And to be safe, I also deactivated the plugin that did sorting on the Priority code. It didn't make a difference.

Did I miss something?

Hi Anne,

Let's try modifying the code a bit.

try this one:

add_action( 'pre_get_posts', 'posts_in_category' );
function posts_in_category( $query ) {
	if( $query->is_main_query() && is_category( 'tutorial' ) && ! is_admin() ) {
		$query->set( 'posts_per_page', '10' );
	}
}

You then check the page in incognito mode. :D

Hi Elvin,

thanks, but still no luck. And yes, I cleared the cache and reviewed it in an Incognito window. In both snippets, it seems to be returning what I want, but the page navigation still exists. If I click Page 2, the page refreshes but not the URL. I'm looking at the same top 10. However, if I click the icon for Page 9, it does go to /tutorials/page/9.

I'm thinking I may just use CSS to hide the other pages with Simple CSS. It works on my staging site. I just need to think through to see if there are any unintended consequences.

/* Remove bottom nav on Tutorials page */
.category-tutorials #nav-below {
display: none;
}

To clarify: Was the goal to display only 10 post and remove the page navigation regardless of how many post exists under a category?

If that's the case then your CSS would be fine.

Yes, the goal was to show only 10 entries so no page navigation would show.

Try this:

add_action( 'pre_get_posts', 'posts_in_category' );
function posts_in_category( $query ) {
	if( $query->is_main_query() && is_category( 'tutorial' ) && ! is_admin() ) {
		$query->set( 'posts_per_page', '10' );
		$query->set( 'no_found_rows', true );
	}
}

Thanks. That works. I no longer have to use CSS to hide pagination.

No problem. :D

This archived topic is closed to new replies.