Community Forums › Forums › Archived Forums › Design Tips and Tricks › Limit number of posts in a category template
Tagged: category template, limit posts
- This topic has 10 replies, 3 voices, and was last updated 9 years, 8 months ago by
deejuk.
-
AuthorPosts
-
July 10, 2015 at 2:55 am #158999
deejuk
MemberHi.
I have a simple category template which simply changes the position of the featured image and size. It works fine.
However I want to limit the number of posts that is shown for that category alone. I have achieved this by adding the following to functions.php but wondered if there was a way of keeping the code in the category template rather than having to remember to go to functions.php each time I want to change the limit.
The code I am using in functions.php is
`// Change posts per page in the design category
add_action( 'pre_get_posts', 'cat_posts_per_page' );
function cat_posts_per_page( $query ) {
if( $query->is_main_query() && is_category( '604' ) && ! is_admin() ) {
$query->set( 'posts_per_page', '12' );
}
}Hope this makes sense
July 10, 2015 at 6:34 am #159008Davinder Singh Kainth
MemberCheck this - https://amethystwebsitedesign.com/change-the-posts-per-page-in-your-portfolio-category-template/
Sunshine PRO genesis theme
Need Genesis help? Davinder @ iGuiding Media | My Blog | Fresh Genesis ThemesJuly 10, 2015 at 6:37 am #159009deejuk
MemberHi yeh it requires the code to go into functions.php I am looking for the code to site my my category-xx.php file
July 11, 2015 at 7:50 am #159087Davinder Singh Kainth
MemberFor such implementation, code has to be places in functions.php file.
Also refer this - http://wordpress.stackexchange.com/questions/47861/different-number-of-posts-in-each-category
Sunshine PRO genesis theme
Need Genesis help? Davinder @ iGuiding Media | My Blog | Fresh Genesis ThemesJuly 12, 2015 at 8:09 pm #159228coralseait
MemberNothing stops you from adding something like that into your cat template instead of functions.php. Depending on theme / data setup you may need to do is_category and/or is_tax conditional. See:
https://codex.wordpress.org/Function_Reference/is_category
https://codex.wordpress.org/Function_Reference/is_tax
July 12, 2015 at 8:19 pm #159231coralseait
MemberOh yeah, one note, in Templates we tend to just forego pre_get_posts and write a WP_Query / WP_Queries, as typically we have more to override than simple pagination or other things. Have found it easier to use WP_Query all the time instead of pre_get_posts filtering because often we'll end up customising the query more as matter of typical dev. In that case you just replace the genesis loop somehow.
You have a couple options, you could do genesis_custom_loop() and set the query args there or do a full custom loop with full custom WP_Query yourself. Up to to you, but these should get you started.
http://genesissnippets.com/genesis-custom-loop/
http://www.carriedils.com/custom-page-template-genesis/An edit: just to be clear, you can use a custom page template OR the wp template hierarchy with the above; doesn't matter, I should have mentioned that as Carrie's tutorial mentions the custom page template method but the code for wp query is the same no matter.
July 13, 2015 at 2:49 am #159245deejuk
MemberHi all thanks for the replies.
I tried adding the same bit of code into my cat template but it doesn't work, although it works in the functions.php file
July 13, 2015 at 3:46 am #159246coralseait
MemberBy didn't work what do you mean? No changes, or nothing returns, etc?
You may need remove_action first on pre_get_posts and then add_action with it.
WP_Query will work regardless with a custom loop in your template.
July 13, 2015 at 4:35 am #159248deejuk
MemberWhat I mean is when the above code is functions.php it limits the number of shown in cat 604
However if I place the same code in the cat 604 template file and remove is from the functions.php the post limit goes back to default post count.
July 13, 2015 at 6:01 pm #159328coralseait
MemberI think you'll probably need a remove_action in your template, then add it. If that doesn't work and you need help with either genesis_custom_loop() or a custom WP_Query loop, let me know and I'm happy to help you!
July 14, 2015 at 2:09 am #159356deejuk
MemberThank you for that. I have had a play with it and can't get it to work in the category temp, but it works in functions.php - so all I suppose I will leave it until I have more time to learn some more.
Thank you all for the replies.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.