• Hey everybody,

    I’m trying to load a custom post after every 6 regular posts on every page. The code below works, but only for the first page. As soon as the second page is loaded, the new posts are exactly the same as before and no new custom posts are loading. It seems like the pagination is broken. To be honest, I’m not getting the WordPress pagination and I’m stuck here. Does anybody have a clue how to solve the problem.

    Kind regards

    
    <main id="main" class="site-main grey lighten-4" role="main"> 
        <p class="flow-text center grey lighten-4">Neueste Beitr?ge</p>
            
        <div id="primary" class="container grey lighten-4 feed-main">
        <div class="row">
                          
        <?php      
        // Initialize counters for the number of posts 
        $post_counter = 1;
            //$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        
        // Initialization for the main query and the query for the inner loop
        $post_args = array(
                        'post_type'=>'post',
                        'posts_per_page' => 12,
                        'paged' => $paged
                        );       
        $post_query = new WP_Query($post_args);
                
        $collection_args = array(
                                'post_type' => 'nls_collection',
                                'posts_per_page' => 2,
                                'paged' => $paged
                                );
        $collection_query = new WP_Query($collection_args);
    
        // Start main loop to load only posts of the type 'post'
        while ($post_query->have_posts()) : $post_query->the_post();
            
            // Load template file
            get_template_part('template-parts/content');
            
            // backup the current $post
            $backup_post = $post;
            global $post;
            
            // Prepare inner loop only after every 6 posts
            if($post_counter != 0 && $post_counter % 6 == 0) :
                
                if($inner_backup): $post = $inner_backup; endif;
                // Start inner loop
                if($collection_query->have_posts()) : $collection_query->the_post();
                    // Load the title ?>
                    </div>
                    
                    <div class="row">
                        <div class="col s12 red">                       
                        <h1><?php echo the_title(); ?></h1> 
                    </div>
                    </div>               
                    <div class="row">
                <?php 
                //$inner_backup = $post;
                //global $post;
                endif;
           
            endif;
             
            // restore the global $post from the previously created backup and increment post counter
            $post = $backup_post;
            wp_reset_query();
            $post_counter++;
                
        endwhile;
        
        // Set the pagination args.
        $paginateArgs = array(
            'format'  => '?paged1=%#%',
            'current' => $paged1, // Reference the custom paged query we initially set.
            'total'   => $post_query->max_num_pages // Max pages from our custom query.
        ); ?>
        <!-- Wrap the pagination -->
        <div class="nlsnavigation">
            <?php echo paginate_links( $paginateArgs ); ?>
        </div>
        <?php 
        // Previous/next page navigation.
        //<?php the_posts_pagination( array(
        //    'prev_text'          => __( 'Previous page', 'nlscustom' ),
        //    'next_text'          => __( 'Next page', 'nlscustomn' ),
         //   'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'nlscustom' ) . ' </span>',
        //) );
       
        ?>
    		
    
            
            </div>
    
  • The topic ‘pagination for custom query within custom query not working’ is closed to new replies.