Support » Plugin: cbnet Different Posts Per Page » cbnet DiffPosts per page category choice

Viewing 1 replies (of 1 total)
  • Plugin Author Chip Bennett

    (@chipbennett)

    I’m looking for a good way to implement that feature.

    Version 2.0 of the Plugin was completely rewritten from previous versions, and the Plugin now works in a completely different (and far better) way. One of the things I dropped, unless/until I can come up with a clean way to re-implement it, was the ability to add arbitrary categories.

    If I can come up with a clean way to re-implement it, I will. But in the meantime, it is easy enough to do yourself, using the following example code.

    Assuming you have a category slug foo, and you want it to display 5 posts per page, in ascending order:

    function anemih_filter_pre_get_posts( $query ) {
        if ( is_category( 'foo' ) && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '5' );
            $query->set( 'order' => 'ASC' );
        }
    }
    add_filter( 'pre_get_posts', 'anemih_filter_pre_get_posts', 11 );

    You would add this code to your Theme’s functions.php file, or, better yet, a site-specific Plugin. Just change foo, 5, and ASC as needed.

    If you have a second category, bar, that you want to display 3 posts per page, in descending order, you can simply add it to the function, like so:

    function anemih_filter_pre_get_posts( $query ) {
        if ( is_category( 'foo' ) && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '5' );
            $query->set( 'order' => 'ASC' );
        }
        if ( is_category( 'bar' ) && $query->is_main_query() ) {
            $query->set( 'posts_per_page', '3' );
            $query->set( 'order' => 'DESC' );
        }
    }
    add_filter( 'pre_get_posts', 'anemih_filter_pre_get_posts', 11 );

    That’s all that the Plugin is doing, actually. 🙂 (And I added a priority 11 to the add_action() call, to ensure that your custom code fires after the Plugin code.)

Viewing 1 replies (of 1 total)
  • The topic ‘cbnet DiffPosts per page category choice’ is closed to new replies.