- This topic has 9 replies, 3 voices, and was last updated 4 years, 6 months ago by
Elvin.
-
AuthorPosts
-
December 9, 2021 at 4:14 pm #2044136
Anne
I have a category on my site called Tutorials. Underneath it, are various sub-categories, Excel, Google, etc.
Tutorials
– Excel
– Google
– Email
– EtcInstead 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.
December 9, 2021 at 5:54 pm #2044167Ying
StaffCustomer SupportHi 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 🙂
December 9, 2021 at 6:59 pm #2044196Anne
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?
December 9, 2021 at 8:40 pm #2044238Elvin
StaffCustomer SupportHi 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. 😀
December 10, 2021 at 9:23 am #2044993Anne
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;
}December 12, 2021 at 4:31 pm #2046893Elvin
StaffCustomer SupportTo 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.
December 12, 2021 at 4:34 pm #2046896Anne
Yes, the goal was to show only 10 entries so no page navigation would show.
December 12, 2021 at 4:38 pm #2046898Elvin
StaffCustomer SupportTry 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 ); } }December 12, 2021 at 4:47 pm #2046899Anne
Thanks. That works. I no longer have to use CSS to hide pagination.
December 12, 2021 at 6:57 pm #2046953Elvin
StaffCustomer SupportNo problem. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.