WordPress.org

Forums

Custom Post Type UI
[resolved] Change number of posts on category pages? (10 posts)

  1. jmarfs
    Member
    Posted 9 months ago #

    Hi.

    I'm trying to change the number of pages that display on category pages, e.g.:

    http://movingbodyresources.com/services/somatic-movement-therapy/

    There doesn't seem to be anywhere in the control panel or plugin options that allows me to change this. Do I have to do it in one of the PHP files, and if so, which one?

    Thanks.

    https://wordpress.org/plugins/custom-post-type-ui/

  2. Michael Beckwith
    Member
    Plugin Author

    Posted 9 months ago #

    We don't actually control this type of thing. Our plugin is meant only to register the post types and taxonomies, not control any of the output from the content you create with the post types/taxonomies you register.

    That said, I believe by default it's controlled by the "Posts per page" setting in your Settings > Reading area, but that is also a setting used by other parts of your website, so changing it would affect more than just your category archive.

    Best I have for your case is a bit technical, and I'm not sure about your comfort level with php.

    Combine the example from http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Changing_the_number_of_posts_per_page.2C_by_post_type with an if statement check for is_category() instead of the example's is_post_type()

  3. jmarfs
    Member
    Posted 9 months ago #

    Hi Michael,

    Thanks for the response. I've already tried Settings>Reading but has made no difference.

    I thought I might have to change something in PHP, but am hesitant to make any changes to PHP files. I would if I knew exactly what to do (or even which PHP file to edit!)

    If it's a simple matter of injecting the code into a file, I'm sure I can handle that (I need to improve my understanding of PHP anyway).

    Can you give some slightly more specific instructions?

    Thanks!

  4. Michael Beckwith
    Member
    Plugin Author

    Posted 9 months ago #

    function conditional_custom_category_limit( $query ) {
    
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_category() ) {
            $query->set( 'posts_per_page', 1 );
        }
    }
    add_action( 'pre_get_posts', 'conditional_custom_category_limit' );

    set 1 to however many you want. This should go in your theme's functions.php file. Do be careful editing that file, especially if you're doing it through the WP Admin, as php errors can cause a white screen and you won't be able to get back in until you get the error resolved via FTP or so.

  5. jmarfs
    Member
    Posted 9 months ago #

    I inserted that and fortunately had no php errors/white screen but it doesn't appear to have worked. Although I upped the number of posts to 200, it's still only displaying 5.

    Would it have anything to do with the fact that my custom categories are labeled as 'services' instead of categories? E.g:

    http://movingbodyresources.com/services/martial-arts/

    Thanks again.

  6. Michael Beckwith
    Member
    Plugin Author

    Posted 9 months ago #

    So you're saying this is a custom taxonomy, and not the default made "Categories" ? That would actually play a part in this working.

  7. jmarfs
    Member
    Posted 9 months ago #

    Sorry if I wasn't clear about that, Michael. Yes, the 'categories' I'm talking about are actually custom taxonomies built using the Custom Post Types UI plugin. The one I'm particularly concerned with at the moment is 'services', as you can see in the URLs above.

  8. Michael Beckwith
    Member
    Plugin Author

    Posted 9 months ago #

    try is_tax instead of is_category and pass in the taxonomy slug

  9. jmarfs
    Member
    Posted 9 months ago #

    Tried changing is_category to is_tax but that didn't work, so tried is_services & is_service instead but still no dice.

    What do you mean by 'pass in the taxonomy slug'?

    Sorry--I'm a total PHP beginner!

  10. Michael Beckwith
    Member
    Plugin Author

    Posted 9 months ago #

    if ( is_tax( 'service' ) ) {
        $query->set( 'posts_per_page', 1 );
    }

    May also be a case of doing $query->is_tax( 'service' ) if the first version doesn't work. Worth tinkering with.

Reply

You must log in to post.

About this Plugin

  • Custom Post Type UI
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic