• Hi.

    Wondering if anyone can help with a project I am working on.

    I have 10 posts showing on my blog page (index.php).

    At the top is a loop that only shows one result and shows popular posts beside it.This block is available on every page that is paginated too.

    Underneath is 9 other posts using a second loop and pagination. These posts change with pagination.

    I am looking to get a few advanced features though and was wondering if anyone can help.

    1. I am looking for an option that says something similar to “Results per page:” and then allows the user to select from a dropdown to change how many posts are shown.

    2. if there is a way I can get it to display something like “1-10 out of total posts and then update with the pagination.

    If these things are possible I’d love to know how.

    My code is

    <main id="main" class="site-main">
        <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
        <div class="recent">
            <div>
            <?php
            $args = array(
                'posts_per_page' => '1',
                'ignore_sticky_posts' => true
            );
            $query = new WP_query ( $args );
            if ( $query->have_posts() ) : 
    		//if ( have_posts() ) :
                /* Start the Loop */
                while ( $query->have_posts() ) : $query->the_post();
    			//while ( have_posts() ) :
                    //the_post();
                    
                    get_template_part( 'template-parts/content', get_post_type() );
    			endwhile;
                rewind_posts();
                // the_posts_navigation();
                
    		else :
    
    			get_template_part( 'template-parts/content', 'none' );
    
    		endif;
            ?>
            </div>
            <div>
            <h3>Popular Posts</h3>
    </div>
            </div>
    <div class="posts">
            <?php while ( have_posts() ) : the_post(); ?>
     
    <?php get_template_part( 'template-parts/content', get_post_type() ); ?>
        
    <?php // End the loop.
    endwhile; ?>
    
    </main><!-- #main -->
    <?php custom_pagination(); ?>
    <?php
    get_sidebar();
    get_footer();
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    If it can be done with PHP, it’s possible, so yes!

    A user selected value needs to be passed as an URL parameter so the main query knows how many posts per page to get. Set the passed value as the “posts_per_page” query var in the “pre_get_posts” action callback.

    The total posts can be had from the global $wp_query object (when your custom query is not in effect) as its found_posts property. You can calculate which posts are displayed from the “paged” query var and the “posts_per_page” value. For example, if paged == 3 and posts per page == 5, the page is showing posts 10-14. Be sure the last value (14) does not exceed the found_posts value in case we’re on the final page.

    BTW, when your custom query is finished you should call wp_reset_postdata(); to properly restore the main query’s globals.

    Thread Starter fraserc89

    (@fraserc89)

    Hi @bcworkz. Sorry for the late reply. Everything has been up in the air for obvious reasons. Could you explain a little more about the pre_get_posts please?
    Will that allow me to perform another request to the database and update the page from a dropdown menu?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Advanced posts results’ is closed to new replies.