Support » Fixing WordPress » WordPress multi loop and pagination

  • Resolved underpk

    (@underpk)


    Hi I’m currently working on this project
    bsearch

    <?php
                $args = array( 'posts_per_page' => 2, 'category' => 5 );
                $myposts = get_posts( $args );
                foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    
                        // content of the first 2 post here //
    
                <?php endforeach; wp_reset_postdata();?>
    
                <?php
                $args = array( 'posts_per_page' => 4, 'category' => 5,'offset' => 2 );
                $myposts = get_posts( $args );
                foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    
                        // content of the last 4 post here //
    
                <?php endforeach; wp_reset_postdata();?>

    I searched and I knew that by this way the page pagination will not work. But I only know a few basic in wordpress and php coding.

    Can anyone help me solve this problem please?
    Here is the design I made in PSD

Viewing 2 replies - 1 through 2 (of 2 total)
  • Marvel Labs

    (@royalprince)

    Install a pagination plugin, and see if it fetches you desired results.

    I get to see from your code that you want to display only four posts of category 5 in single page and then paginate it.

    If this is your front page, you could go to WordPress>Settings> Reading

    and set blog posts to 4

    Michael

    (@alchymyth)

    no need really to use two (foreach) loops for different styling or output of the two first posts.

    try to work with WP_Query() and a while loop, making use of conditional statements based on the buld-in loop counter ->current_post

    example:

    $args = array( 'posts_per_page' => 6, 'category' => 5, 'paged => get_query_var('paged') );
                $myposts = new WP_Query( $args );
                if( $myposts->have_posts() ) :
                while( $myposts->have_posts() ) :
                $myposts->the_post(); 
    
                if( $myposts->current_post <= 1 ) : ?>
    
                        // content of the first 2 post here //
    
                <?php else : ?>
    
                        // content of the last 4 post here //
    
                endif; ?>
    
                <?php endwhile; endif; wp_reset_postdata(); ?>

    review
    https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

    the example might get more complicated if you want/need to wrap the two groups of posts each into different html containers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WordPress multi loop and pagination’ is closed to new replies.