phpwordpresspaginationcustom-post-type

pagination for custom post type wordpress


i stock with pagination i tried a lot of solutions but nothing worked!i use WordPress 5.4.2 and my website is a onepage web and of course i want to use this in my index.php

here is my code:

                    <?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$portfolio_post_args = array(
    'post_type'=>'portfolio',
    'posts_per_page' => 2,
    'paged' => $paged,
);
                                $portfolio_post = new WP_Query($portfolio_post_args);

                                 while($portfolio_post->have_posts()) :$portfolio_post->the_post();?>
                    <div id="portfolio1" class="portfolio-layout1 onscroll-animate">
                        <article class="portfolio-item filter-lifestyle">
                            <div class="popup-window-trigger" data-popup="#<?php echo 'portfolio-pic-'.get_the_ID( ); ?>">
                                <img class="onscroll-animate" alt="img1" src="<?php echo get_the_post_thumbnail_url(); ?>"
                                    data-animation="fadeInUp">
                                <div class="portfolio-detail">
                                    <div class="portfolio-detail-container">
                                        <div class="portfolio-detail-content">
                                            <h2><?php echo get_the_title( ); ?></h2>
                                            Lifestyle, Other
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>

                        <?php endwhile;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $portfolio_post_args->max_num_pages
) );
?>

I'm sorry if it's highly repetitive question - none of other fixes I found worked for me. please someone tell me where is the problem?

Thanks a lot!

Edit 1 i try this and it's not work neither !

                    <?php
$temp = $wp_query;
$wp_query= null;
$args = array(
    'post_type'   =>   array('portfolio'),
    'paged'       =>$paged,
    'post_per_page' => 6,
     );
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
                    <div id="portfolio1" class="portfolio-layout1 onscroll-animate">
                        <article class="portfolio-item filter-lifestyle">
                            <div class="popup-window-trigger" data-popup="#<?php echo 'portfolio-pic-'.get_the_ID( ); ?>">
                                <img class="onscroll-animate" alt="img1" src="<?php echo get_the_post_thumbnail_url(); ?>"
                                    data-animation="fadeInUp">
                                <div class="portfolio-detail">
                                    <div class="portfolio-detail-container">
                                        <div class="portfolio-detail-content">
                                            <h2><?php echo get_the_title( ); ?></h2>
                                            Lifestyle, Other
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                        <?php endwhile; ?>
<div class="navigation">
  <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
  <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>
<?php $wp_query = null; $wp_query = $temp;?>

Solution

  • problem solved:

                <?php 
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $data= new WP_Query(array(
        'post_type'=>'portfolio', // your post type name
        'posts_per_page' => 6,
        'paged' => $paged,
    ));
    
    if($data->have_posts()) :
        while($data->have_posts())  : $data->the_post();?>
    
                        <article class="portfolio-item filter-lifestyle">
                            <div class="popup-window-trigger" data-popup="#<?php echo 'portfolio-pic-'.get_the_ID( ); ?>">
                                <img class="onscroll-animate" alt="img1" src="<?php echo get_the_post_thumbnail_url(); ?>"
                                    data-animation="fadeInUp">
                                <div class="portfolio-detail">
                                    <div class="portfolio-detail-container">
                                        <div class="portfolio-detail-content">
                                            <h2><?php echo get_the_title( ); ?></h2>
                                            Lifestyle, Other
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
    
                <?php endwhile;?>
                <?php endif;
                     wp_reset_postdata();?>
                    </div><!-- #portfolio -->
                <!-- Start Pagination - WP-PageNavi -->
                <br>
                <div id="pagination" class="nav-links">
                    <?php        if ( $data->max_num_pages > 1 ) :
                          $big = 999999999;
                          echo '<div class="pagination">';
                          echo paginate_links( array(
                              'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                              'format' => '?paged=%#%',
                              'current' => max( 1, get_query_var('paged') ),
                              'total' => $data->max_num_pages,
                              'prev_text' => 'Newer',
                              'next_text' => 'Older'
                          ) );
                          echo '</div>';?>
                </div>
                <!-- End Pagination -->
                <?php endif;?>