• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Limit number of posts in a category template

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Limit number of posts in a category template

This topic is: not resolved

Tagged: category template, limit posts

  • This topic has 10 replies, 3 voices, and was last updated 11 years ago by deejuk.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • July 10, 2015 at 2:55 am #158999
    deejuk
    Member

    Hi.

    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


    WN6 Web Design – North West of England

    July 10, 2015 at 6:34 am #159008
    Davinder Singh Kainth
    Member

    Check 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 Themes

    July 10, 2015 at 6:37 am #159009
    deejuk
    Member

    Hi yeh it requires the code to go into functions.php I am looking for the code to site my my category-xx.php file


    WN6 Web Design – North West of England

    July 11, 2015 at 7:50 am #159087
    Davinder Singh Kainth
    Member

    For 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 Themes

    July 12, 2015 at 8:09 pm #159228
    coralseait
    Member

    Nothing 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


    Coral Sea IT

    July 12, 2015 at 8:19 pm #159231
    coralseait
    Member

    Oh 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.


    Coral Sea IT

    July 13, 2015 at 2:49 am #159245
    deejuk
    Member

    Hi 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


    WN6 Web Design – North West of England

    July 13, 2015 at 3:46 am #159246
    coralseait
    Member

    By 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.


    Coral Sea IT

    July 13, 2015 at 4:35 am #159248
    deejuk
    Member

    What 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.


    WN6 Web Design – North West of England

    July 13, 2015 at 6:01 pm #159328
    coralseait
    Member

    I 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!


    Coral Sea IT

    July 14, 2015 at 2:09 am #159356
    deejuk
    Member

    Thank 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.


    WN6 Web Design – North West of England

  • Author
    Posts
Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble