Add pagination to archive.php template - php

I have created an archive template within my bespoke Wordpress site but I need to add pagination - I have tried different plugins/code but nothing seems to work. I currently have 6 posts setup on the backend but I have limited it to 3 posts per page for testing purposes. Existing code below:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 3,
'paged' => $paged
);
$posts_query = new WP_Query($args);
if($posts_query->have_posts()) { ?>
<div class="index-grid">
<div class="grid-column">
<?php while($posts_query->have_posts()) {
$posts_query->the_post(); ?>
<div class="related-item">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('header_image'); ?>" />
<h3 class="title"><?php the_title(); ?></h3>
<p class="blurb"><?php the_field('introduction'); ?></p>
</a>
</div>
<?php } ?>
</div>
</div>
<?php wp_reset_postdata();
}
?>
</section>```
Any help would be greatly appreciated!
Thanks

Try this:
<div class='pagination'>
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $posts_query->max_num_pages
));
?>
<div>

Related

Wordpress pagination issue - I set 'posts_per_page' which works for every page except for page 1 which is just showing all posts

I have the following set up in my template file:
<div class="flex" id="allPosts">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'posts_per_page' => 12,
'paged' => $paged,
'order' => 'DESC',
);
$wp_query = new WP_Query( $args ); ?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query -> have_posts()) : $wp_query -> the_post(); ?>
<div class="blog-entries">
<?php if (has_post_thumbnail()) {
$featuredImg = get_the_post_thumbnail_url();
} else {
$featuredImg = get_stylesheet_directory_uri() . '/img/blk-fri.jpg';
} ?>
<a class="blog-module" href="<?php the_permalink(); ?>">
<div class="blog-img" style="background-image: url('<?php echo $featuredImg; ?>');">
<div>
<div class="author">By <?php the_author(); ?></div>
<div class="date">
<p><?php echo get_the_date('M'); ?></p>
<p><?php echo get_the_date('d'); ?></p>
</div>
</div>
</div>
<div class="blog-content">
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
</div>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
This functionality is working, except on page one. Every other page limits the amount of posts seen to 12 as specified in the 'posts_per_page', but page 1 is just showing every post. Ive tried searching around for a similar problem, but have come up short. I would appreciate any and all guidance on what might be happening here, thanks!
You can view the issue here: http://listingmirror.devsite.work/blog/
try in that form
// WP_Query arguments
$args = array(
'nopaging' => false,
'paged' => '12',
'posts_per_page' => '12',
'posts_per_archive_page' => '12',
'order' => 'DESC',
'orderby' => 'title',
);
// The Query
$paged = new WP_Query( $args );
// The Loop
if ( $paged->have_posts() ) {
while ( $paged->have_posts() ) {
$paged->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
The imported posts from the client were all marked 'sticky' in Wordpress. This was overwriting the post_per_page argument.

Wordpress - Pagination doesn't work with custom loop but working in blog pages

I have a loop with custom post types, and pagination doesn't appear, when I enter the URL with /page/2, /page/3... it shows the content correctly, but links don't appear on the page.
Here is the code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$parent_only_query = new WP_Query(array(
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
));
while ($parent_only_query->have_posts()){
$parent_only_query->the_post();
//content
}
pagination(); ?>
Archive page with pagination working:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //content ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?>
<?php endif; ?>
<?php pagination(); ?>
add this in your functions.php
function pagination_nav() {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) { ?>
<nav class="pagination" role="navigation">
<div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div>
</nav>
<?php }
}
display on page.php
<?php pagination_nav(); ?>
pagination can be shown in custom post type archive template and on custom template as well.
Pagination for archive template.
// current page
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array( 'post_type' => 'product',
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
new WP_Query( $args );
// Call pagination function before wp_reset_postdata()
the_posts_pagination( array(
'prev_text' => '<span class="fa fa-angle-left" aria-hidden="true"></span>',
'next_text' => '<span class="fa fa-angle-right" aria-hidden="true"></span>',
'screen_reader_text' => ' ',
'before_page_number' => '',
'mid_size' => 3,
) );
Pagination for custom Template
// Get current page.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array(
post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
$query = new WP_Query( $args );
$totalPage=$query->max_num_pages;
$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' => $totalPage
) );
You can check WordPress official document on Wordpress Codex

pagination doesn't work on a single page of WordPress

Pagination doesn't work on a single page of WP.
In the code below, everything looks great except the pagination shows the same contents on the next page.
I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_cat',
'field' => 'slug',
'terms' => array( 'disney' )
),
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
I expect the pagination to work correctly instead of showing the same contents on all pages.
As you see, I want to make the pagination work only on the posts with "disney" slug.
You can replace the code instead of your old code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
Now, you can check it that pagination is working or not.

Wordpress custom post type pagination shows same content on each page

I have a custom post type name portfolio.
I have applied pagination successfully but content on each page is the same.
Below is my code.
I have tried too many solutions but unable to find appropriate answer.
please help
<?php
$exec_query = new WP_Query( array (
'posts_per_page'=>10,
'post_type' => 'portfolio',
'job_role' => 'executive',
'post-thumbnails' => 'thumbnail',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1) );
// The Loop
if ( $exec_query->have_posts() ) {
while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
<div>
// The content
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $exec_query->max_num_pages
) );
wp_reset_postdata();
}
?>
Try this code,
$the_query = new WP_Query( array('posts_per_page'=>10,
'post_type'=>'phcl',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php echo $file; ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
Try this
<?php
/*
Template name: Test portfolio
*/
$exec_query = new WP_Query( array (
'posts_per_page'=>10,
'post_type' => 'portfolio',
'job_role' => 'executive',
'post-thumbnails' => 'thumbnail',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1) );
if ( $exec_query->have_posts() ) {
while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
<p><?php echo get_the_title(); ?></p>
<div><?php the_content(); ?></div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $exec_query->max_num_pages
) );
wp_reset_postdata();
}
?>

WordPress custom pagination for custom posts not working

I'm developing custom WordPress theme, in that, i'm using custom queries for fetching data and also using pagination for the bulk data.
I have written code for pagination and it is also showing pagination below posts.
my concern is: whenever I click on 2 page (Pagination), it is showing the same post of page 1 (Pagination).
Please check with below code for your reference:-
<div class="blog_area container">
<div class="wrapper row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'video',
'order' => 'ASC',
'posts_per_page' => 1,
'paged' => $paged
);
$the_query = new WP_Query($args);
// The Query
while ($the_query->have_posts()):
$the_query->the_post();
$vdoLink = simple_fields_value('vdo_link');
$showVdo = $vdoLink;
?>
<div class="main_wrapper col-lg-12 col-md-12 row_wrapper">
<div class="col-md-6">
<div class='embed-container'>
<iframe src="<?php echo $showVdo;
?>" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div class="col-md-6">
<div class="row ">
<h2 class="margin-top-zero">
<b><?php the_title(); ?></b>
</h2>
</div>
<div class='row'>
<p><?php the_content('Read More');
?></p>
</div>
</div>
</div>
<?php
$post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
?>
<div class="row">
<div class="pagination">
<?php
echo paginate_links(array(
// 'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $the_query->max_num_pages
));
?>
</div>
</div
</div>
</div>
you can try this code.
<?php
$args = array(
'order' => 'DESC',
'showposts' => 1,
'post_type' => 'video',
'paged' => $paged
);
$the_query = new WP_Query($args);
// The Query
if($the_query->have_posts()) : while($the_query->have_posts()): $the_query->the_post(); ?>
<?php the_title(); ?>
<?php
wp_reset_query();
endwhile; else :
echo "no posts!";
endif;
?>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
// 'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
));
?>

Categories