WordPress.org

Ready to get started?Download WordPress

Forums

cbnet Different Posts Per Page
[resolved] cbnet DiffPosts per page category choice (2 posts)

  1. anemih
    Member
    Posted 2 years ago #

    I cannot choose which category I want to short ASC or DEC. Something is different at this version. How can I have a menu like before? I need it ASAP cause my site collapsed.

    http://wordpress.org/extend/plugins/cbnet-different-posts-per-page/

  2. Chip Bennett
    Theme Review Admin
    Plugin Author

    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.