Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Leanne D
Leanne D
4,550 Points

WordPress: CPT posts per page

I have a WordPress CPT with custom taxonomies. I want the mobile to show all posts on one page, but tablet and desktop to only show 6 posts per page.

I have some php to do this:

function aa_order_cpt( $query ) {

if ( is_admin() || ! $query->is_main_query() ) {
    return;
}

if ( is_archive() ) {
    $query->set( 'order' , 'des' );
    $query->set( 'orderby', 'date');
    $query->set('posts_per_page', -1 ); // mobile - all

    if( $query->is_main_query() && ! wp_is_mobile() ) {
        $query->set('posts_per_page', 6);
    }

    return;
}

} add_action( 'pre_get_posts', 'aa_order_cpt', 1 );

The code doesn't work because wp_is_mobile isn't great and uses this for tablets too.

I need to determine the width of the viewport, which I know I can do with jQuery, but how do I set the posts per page using jquery?

Thanks