Pagination does not display WP Query - wordpress

I can't seem to get my pagination working.
I have been through many similar questions on Stack Overflow and tried to modify it but nothing seems to work.
EDIT - I'm trying to get numbered pagination - I'm not even sure the
examples I have tried are numbered.
My code without any attempts is
<section id="blog-posts" class="latest-blog-posts full-width standard-padding">
<div class="full-width-inner">
<div class="the-posts">
<?php
// Define our WP Query Parameters
$the_query = new WP_Query( 'posts_per_page=9' ); ?>
<?php
// Start our WP Query
while ($the_query -> have_posts()) : $the_query -> the_post();
// Display the Post Title with Hyperlink
?>
<div class="post">
<div class="post-thumb">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" alt="" />
</div>
<h2 class="post-title-teaser"><?php the_title(); ?></h2>
<div class="post-date-preview"><p><?php echo get_the_date(); ?></p></div>
<div class="post-excerpt"><?php
the_excerpt(); ?></div>
<div class="elc-button post-button"><a class="read-on elc-button" aria-label="read more of the article about '<?php the_title(); ?>'" href="<?php the_permalink() ?>">Read More</a></div>
</div>
<?php
// Repeat the process and reset once it hits the limit
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</section>
My latest attempts at adding pagination are
<section id="blog-posts" class="latest-blog-posts full-width standard-padding">
<div class="full-width-inner">
<div class="the-posts">
<?php
if ( ! function_exists( 'pagination' ) ) :
function pagination( $paged = '', $max_page = '' ) {
$big = 999999999; // need an unlikely integer
if( ! $paged ) {
$paged = get_query_var('paged');
}
if( ! $max_page ) {
global $wp_query;
$max_page = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
}
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $max_page,
'mid_size' => 1,
'prev_text' => __( '«' ),
'next_text' => __( '»' ),
'type' => 'list'
) );
}
endif;
?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) :
?>
<div class="post">
<div class="post-thumb">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" alt="" />
</div>
<h2 class="post-title-teaser"><?php the_title(); ?></h2>
<div class="post-date-preview"><p><?php echo get_the_date(); ?></p></div>
<div class="post-excerpt"><?php
the_excerpt(); ?></div>
<div class="elc-button post-button"><a class="read-on elc-button" aria-label="read more of the article about '<?php the_title(); ?>'" href="<?php the_permalink() ?>">Read More</a></div>
</div>
<?php
// Repeat the process and reset once it hits the limit
endwhile;
pagination( $paged, $loop->max_num_pages); // Pagination Function
endif;
wp_reset_postdata();
?>
</div>
</div>
</section>
The truth is I didn't know if the initial portion (the if function) should be part of the PHP template or in functions PHP but I tried both
I have also tried this
<div class="the-posts">
<?php
// WP_Query arguments
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '9',
'order' => 'DESC',
'orderby' => 'date',
'paged' => $paged
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) { ?>
<div class="post">
<div class="post-thumb">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" alt="" />
</div>
<h2 class="post-title-teaser"><?php the_title(); ?></h2>
<div class="post-date-preview"><p><?php echo get_the_date(); ?></p></div>
<div class="post-excerpt"><?php
the_excerpt(); ?></div>
<div class="elc-button post-button"><a class="read-on elc-button" aria-label="read more of the article about '<?php the_title(); ?>'" href="<?php the_permalink() ?>">Read More</a></div>
</div>
<?php }
} else {
// no posts found
}
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
) );
// Restore original Post Data
wp_reset_postdata();
?>
</div>
But the page won't load and eventually, I get a 503
This kind of thing isn't my forte so I tried all the examples but nothing seemed to work
Any help would be greatly appreciated

need to check the PHP error log details for help, also you can comments out the template part by part, to find out the error happen on which part exactly.

Okay so after trying for days, I actually got it working not long after posting this like so
<section id="blog-posts" class="latest-blog-posts full-width standard-padding">
<div class="full-width-inner">
<div class="the-posts">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged
);
$custom_query = new WP_Query( $args );
while($custom_query->have_posts()) :
$custom_query->the_post();
?>
<div class="post">
<div class="post-thumb">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" alt="" />
</div>
<h2 class="post-title-teaser"><?php the_title(); ?></h2>
<div class="post-date-preview"><p><?php echo get_the_date(); ?></p></div>
<div class="post-excerpt"><?php
the_excerpt(); ?></div>
<div class="elc-button post-button"><a class="read-on elc-button" aria-label="read more of the article about '<?php the_title(); ?>'" href="<?php the_permalink() ?>">Read More</a></div>
</div>
<?php endwhile; ?>
</div>
<?php if (function_exists("pagination")) {
pagination($custom_query->max_num_pages);
} ?>
</div>
</div>
</section>
Functions.php
// numbered pagination
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\">";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"".$i."";
}
}
if ($paged < $pages && $showitems < $pages) echo "Next ›";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
Thanks to this article
https://www.evan-herman.com/how-to-add-numeric-wordpress-pagination/#.Yxy4S-zML2I

Related

Wordpress CPT pagination returns 404 page

I want to add pagination to my CPT wordpress posts. I've seen some of the solutions, but neither I see no pagination or the pagination does not work.
I've added some pagination code, and it shows the correct number of pages but when I click on page number it returns 404 page. The url is also correct.
Here's my code:
<div class="container">
<div class="row">
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
<?php $args = array( 'post_type' => 'Realizacje', 'posts_per_page' => 6, 'category_name' => array('projekty-wnetrz', 'wykonczenia-pod-klucz'), 'paged' => $paged); ?>
<?php $loop = new WP_Query($args); ?>
<?php $count=0; ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
$num = $loop->post_count;
?>
<div class="col-md-4">
<div class="realisation">
<!-- <div class="gallery">
<?php
$images = get_field('galeria');
$size = 'medium';
if( $images ): ?>
<div id="lightgallery<?php echo $count; ?>">
<?php foreach( $images as $image ): ?>
<div class="gal">
<img src="<?php echo $image; ?>"/>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div> -->
<div class="galeria">
<div class="lightg" id="lightgallery<?php echo $count; ?>">
<a href="<?php echo get_the_post_thumbnail_url(); ?>">
<div class="relative-box"><img class="first_image"src="<?php echo get_the_post_thumbnail_url(); ?>"/>
<div class="demo-gallery-poster">
<i class="fa fa-search" aria-hidden="true"></i>
</div></div>
<div class="gallery-title"><h5 class="gallery-t"><?php the_title(); ?></h5></div>
</a>
<?php
$count=$count+1;
$images = get_field('galeria');
$size = 'large';
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<img src="<?php echo $image; ?>"/>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<h5>Brak realizacji</h5>
<?php endif; ?>
<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' => $loop->max_num_pages,
'prev_text' => '«',
'next_text' => '»'
) );
?>
</div>
<?php wp_reset_postdata(); ?>
</div>
</div>

Custom Page Pagination in wordpress

I have placed below function in functions.php file
if ( ! function_exists( 'post_pagination' ) ) :
function post_pagination() {
global $wp_query;
$pager = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
My current page includes this code which will give me pagination based on custom query i have created. I dont get around why query_posts() is not working.
<?php
//Template Name:dummy
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'page',
'paged' => $paged,
'posts_per_page' => 3,
));
if (have_rows('portfolio_listing', 117)) { /* first group */
while (have_rows('portfolio_listing', 117)) {
the_row();
if (have_rows('all', 117)) { /* second group */
while (have_rows('all', 117)) {
the_row(); ?>
<div class="col-lg-4 col-md-6 mt-lg-5">
<a href="<?= the_sub_field('page_link'); ?>" class="portfolio-wrap">
<div class="portfolio-img">
<img src="<?= the_sub_field('portfolio_image'); ?>" alt="">
</div>
<div class="portfolio-content">
<h6><?= the_sub_field('title'); ?></h6>
<h2><?= the_sub_field('content'); ?></h2>
<div>
<button class="portfolio-btn"><?= the_sub_field('btn'); ?></button>
</div>
</div>
</a>
</div>
<?php }
post_pagination();
}
}
}
?>

How to define fallback image for get_the_post_thumbnail_url inside a shortcode

My shortcode is working fine but how to set a fallback image in there? Please suggestion
// display default post as a shortcode with prev/next link
function genericposts_shortcode($atts){
extract( shortcode_atts( array(
'expand' => '',
), $atts) );
global $paged;
$posts_per_page = 10;
$settings = array(
'showposts' => $posts_per_page,
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'ASC',
'paged' => $paged
);
$post_query = new WP_Query( $settings );
$total_found_posts = $post_query->found_posts;
$total_page = ceil($total_found_posts / $posts_per_page);
$list = '<div class="bloglist">';
while($post_query->have_posts()) : $post_query->the_post();
$list .= '
<div class="bloglist__item">
<div class="bloglist__imgwrap">
<img src=" '. get_the_post_thumbnail_url(get_the_ID(),'full') .'">
</div>
<div class="bloglist__wrapt">
<div class="bloglist__datetitle">
<span class="bloglist__date"> '. get_the_date( 'd' ) . '</span>
<span class="bloglist__mnt"> '. get_the_date( 'M' ) . '</span>
<span class="bloglist__yr"> '. get_the_date( 'Y' ) . '</span>
<div class="bloglist__title"> '. get_the_title() .' </div>
</div>
<div class="bloglist__more">
Read More
</div>
</div>
</div>
';
endwhile;
$list.= '</div>';
if(function_exists('wp_pagenavi')) {
$list .='<div class="page-navigation">'.wp_pagenavi(array('query' => $post_query, 'echo' => false)).'</div>';
} else {
$list.='
<span class="prev-posts-links">'.get_previous_posts_link('Previous page').'</span>
<span class="next-posts-links">'.get_next_posts_link('Next page', $total_page).'</span>
';
}
return $list;
}
add_shortcode('post-shortcode', 'genericposts_shortcode');
I have solved the issue below way.
add_shortcode( 'post-shortcode', 'genericposts_shortcode' );
function genericposts_shortcode( $atts ) {
ob_start();
global $paged;
$posts_per_page = 9;
$settings = array(
'showposts' => $posts_per_page,
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'ASC',
'paged' => $paged
);
$post_query = new WP_Query( $settings );
$total_found_posts = $post_query->found_posts;
$total_page = ceil($total_found_posts / $posts_per_page);
if ($post_query->have_posts() ) { ?>
<div class="bloglist">
<?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<div class="bloglist__item" id="post-<?php the_ID(); ?>">
<div class="bloglist__imgwrap">
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('full');?>
<?php } else { ?>
<img src="https://kanooelite.com/upgrade/wp-content/uploads/2020/11/placeholder.png" />
<?php } ?>
</div>
<div class="bloglist__wrapt">
<div class="bloglist__datetitle">
<span class="bloglist__date"> <?php echo get_the_date( 'd' ); ?></span>
<span class="bloglist__mnt"> <?php echo get_the_date( 'M' ); ?></span>
<span class="bloglist__yr"> <?php echo get_the_date( 'Y' ); ?></span>
<div class="bloglist__title"> <?php echo get_the_title(); ?></div>
</div>
<div class="bloglist__more">
Read More
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<span class="prev-posts-links"><?php echo get_previous_posts_link('Previous page'); ?></span>
<span class="next-posts-links"><?php echo get_next_posts_link('Next page', $total_page); ?></span>
<?php $myvariable = ob_get_clean();
return $myvariable;
} ?>
<?php } ?>

WordPress CPT Numbered Pagination

I have a custom post type in WordPress.
The mobile archive page is a slider through ALL the posts.
The desktop archive page is a normal archive page, with numbered navigation and only shows 6 posts per page.
I see to have this working, however on the desktop, there should only be 3 pages, but instead, there are 4 pages, and the fourth page is empty.
I'm baffled as to what I've done wrong, so if anyone can take a look, that would be brill.
Thank you
archive-projects.php:
<?php
if ( !have_posts() ) {
// If no posts match the query
get_template_part( '404' );
return;
}
get_header();
?>
<div class="container">
<div class="content-area mobile" id="mobile-projects-slider">
<?php
$args = array(
'post_type' => 'projects',
'order-by' => 'date',
'order' => 'des',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<article <?php post_class( 'entry entry-archive' ); ?> data-aos="fade">
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="entry-image">
<a href="<?php the_permalink(); ?>">
<div class="post-image" style="background-image: url('<?php echo $backgroundImg[0]; ?>')"></div>
</a>
</div>
<div class="entry-content">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
See More
</div>
</article>
<?php
endwhile;
wp_reset_postdata();
else:
endif;
?>
</div>
<div class="content-area desktop" id="desktop-projects">
<?php
$args = array(
'post_type' => 'projects',
'order-by' => 'date',
'order' => 'des',
'paged' => $paged,
'posts_per_page' => 6
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<article <?php post_class( 'entry entry-archive' ); ?> data-aos="fade">
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="entry-image">
<a href="<?php the_permalink(); ?>">
<div class="post-image" style="background-image: url('<?php echo $backgroundImg[0]; ?>')"></div>
</a>
</div>
<div class="entry-content">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
See More
</div>
</article>
<?php
endwhile;
wp_reset_postdata();
else:
endif;
?>
<?php
get_template_part( '_template-parts/page-navigation' );
?>
</div>
<!-- <div class="aside"> -->
<?php /* get_sidebar(); */ ?>
<!-- </div> -->
</div>
<?php
get_footer();
page-navigation.php:
<?php if ( is_singular() ) : ?>
<?php if( get_post_type() == 'projects' ) { ?>
<div id="navigation" data-aos="fade">
<div class="pagination pagination-singular">
<?php if ( get_next_post() ) { ?>
<div class="nav-next"><?php next_post_link( '%link', 'Previous Project' ) ?></div>
<?php } ?>
<?php if ( get_previous_post() ) { ?>
<div class="nav-previous"><?php previous_post_link( '%link', 'Next Project' ) ?></div>
<?php } ?>
</div>
</div>
<?php } else { ?>
<div id="navigation" data-aos="fade">
<div class="pagination pagination-singular">
<?php if ( get_next_post() ) { ?>
<div class="nav-next"><?php next_post_link( '%link', 'Previous Post' ) ?></div>
<?php } ?>
<?php if ( get_previous_post() ) { ?>
<div class="nav-previous"><?php previous_post_link( '%link', 'Next Post' ) ?></div>
<?php } ?>
</div>
</div>
<?php } ?>
<?php else : ?>
<?php
aa_numeric_posts_nav();
// located in includes > functions > theme-functions.php
?>
<?php endif; ?>
aa_posts_nav_function():
function aa_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
// $paged = 0;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
//
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div id="navigation"><ul class="pagination numbered-pagination">' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li class="nav-prev">%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li class="nav-next">%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
Fixed it. I removed the WP_Query completely for the desktop version (in archive- and added it using a separate function. This is what I have now and works:
archive-projects.php
<?php
if ( !have_posts() ) {
// If no posts match the query
get_template_part( '404' );
return;
}
get_header();
?>
<div class="container">
<div class="content-area mobile" id="mobile-projects-slider">
<?php
$args = array(
'post_type' => 'projects',
'order-by' => 'date',
'order' => 'des',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<article <?php post_class( 'entry entry-archive' ); ?> data-aos="fade">
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="entry-image">
<a href="<?php the_permalink(); ?>">
<div class="post-image" style="background-image: url('<?php echo $backgroundImg[0]; ?>')"></div>
</a>
</div>
<div class="entry-content">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
See More
</div>
</article>
<?php
endwhile;
wp_reset_postdata();
else:
endif;
?>
</div>
<div class="content-area desktop" id="desktop-projects">
<?php
if(have_posts() ) :
while ( have_posts() ) : the_post();
?>
<article <?php post_class( 'entry entry-archive' ); ?> data-aos="fade">
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="entry-image">
<a href="<?php the_permalink(); ?>">
<div class="post-image" style="background-image: url('<?php echo $backgroundImg[0]; ?>')"></div>
</a>
</div>
<div class="entry-content">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
See More
</div>
</article>
<?php
endwhile;
wp_reset_postdata();
else:
echo '<h2>No posts found!</h2>';
endif;
aa_numeric_posts_nav();
?>
</div>
<!-- <div class="aside"> -->
<?php /* get_sidebar(); */ ?>
<!-- </div> -->
</div>
<?php
get_footer();
aa_numeric_posts_nav(); function
function aa_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
// $paged = 0;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
//
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div id="navigation"><ul class="pagination numbered-pagination">' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li class="nav-prev">%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li class="nav-next">%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
function for posts per page CPT for my desktop:
function aa_order_cpt( $query ) {
// exit out if it's the admin or it isn't the main query
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// order category archives by title in ascending order
if ( is_archive() ) {
$query->set( 'order' , 'desc' );
$query->set( 'orderby', 'date');
$query->set('posts_per_page', 6 );
}
return;
}
add_action( 'pre_get_posts', 'aa_order_cpt', 1 );

Pagenation in single agent page not working wordpress

I have created a single agent page in that page i am trying to list all the property by the agent in that property area i have kept an bootstrap pagination that was not working it was showing the page numbers but when you click that was not taking to page1 or page 2 etc. But it was redirecting to the same page. Here is my code.
get_header();
<div class="container-fluid"><div class="row bkg-white bkg-pd-top">
<div class="container">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<div class="col-md-9"><div class="row">
<div class="single-details">
<div class="single-sec-prop-title">Agent Details</div>
<div class="single-prop-detail">
<div class="col-md-4 col-sm-4 col-xs-4"><div class="row"><div class="agent-single-image">
<?php the_post_thumbnail('small-img-featured', array('class' => 'img-responsive')); ?>
</div></div></div>
</div>
</div></div>
</div></div>
<div class="single-details">
<div class="single-sec-prop-title">Description</div>
<div class="single-prop-detail">
<?php the_content(); ?>
</div>
</div>
<div class="col-xs-12"><div class="row">
<div class="single-sec-prop-title agent-single-title">Listed Properties</div>
<?php
endwhile;
endif;
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => array('forsale', 'forrent'),
'numberposts' => -1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => $paged, //very important
'meta_key' => 'select-agent',
'meta_value' => get_the_id(),
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
get_template_part( 'template-parts/property', 'agent' );
endwhile; ?>
<?php
if ($custom_query->max_num_pages > 1) :
$orig_query = $wp_query;
$wp_query = $custom_query;
?><nav class="prev-next-posts">
<?php
if (function_exists('wp_bootstrap_pagination')){
wp_bootstrap_pagination();
}
?>
</nav>
<?php endif;
wp_reset_postdata();
else:
get_template_part( 'template-parts/no', 'post' );
endif; //ends loop
?>
<?php
get_footer();
Here is the link for bootstrap pagenation code Bootstrap pagenatio
Here is a answer for your question. If you need to add an pagenation to your single page for example your single agent page you need to set a template redirect function in wordpress for that you need to add the following code to your active themes function.php In this code please change the agent to your custom post type if your custom post is not agent.
add_filter('redirect_canonical','redirect_single_page');
function redirect_single_page($redirect_url) {
if (is_singular('agent')) $redirect_url = false; // change 'agent' to your custom post type, obviously
return $redirect_url;
}
you can try this with wordpress.
<?php
get_header(); ?>
<div class="container-fluid"><div class="row bkg-white bkg-pd-top">
<div class="container">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<div class="col-md-9"><div class="row">
<div class="single-details">
<div class="single-sec-prop-title">Agent Details</div>
<div class="single-prop-detail">
<div class="col-md-4 col-sm-4 col-xs-4"><div class="row"><div class="agent-single-image">
<?php the_post_thumbnail('small-img-featured', array('class' => 'img-responsive')); ?>
</div></div></div>
</div>
</div></div>
</div></div>
<div class="single-details">
<div class="single-sec-prop-title">Description</div>
<div class="single-prop-detail">
<?php the_content(); ?>
</div>
</div>
<div class="col-xs-12"><div class="row">
<div class="single-sec-prop-title agent-single-title">Listed Properties</div>
<?php
endwhile;
endif;
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => array('forsale', 'forrent'),
'numberposts' => -1,
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => $paged, //very important
'meta_key' => 'select-agent',
'meta_value' => get_the_id(),
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
get_template_part( 'template-parts/property', 'agent' );
endwhile;
customPagination($paged,$custom_query);
endif;
wp_reset_postdata();
else:
get_template_part( 'template-parts/no', 'post' );
endif; //ends loop
?>
<?php
get_footer();
?>
<?php
function customPagination($paged,$probj){
$data = '';
$data .='<nav aria-label="Page navigation example">';
$pager = 999999999;
$pargs = array(
'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $probj->max_num_pages,
'prev_next'=> false,
'type'=> 'array'
);
$links = paginate_links( $pargs );
if ( $links ) :
$data .= '<ul class="pagination justify-content-center">';
// get_previous_posts_link will return a string or void if no link is set.
if ( $prev_posts_link = get_previous_posts_link( __( 'Previous Page' ) ) ) :
$data .= '<li class="prev-list-item">';
$data .= $prev_posts_link;
$data .= '</li>';
endif;
$data .= '<li class="page-item">';
$data .= join( '</li><li class="page-item">', $links );
$data .= '</li>';
// get_next_posts_link will return a string or void if no link is set.
if ( $next_posts_link = get_next_posts_link( __( 'Next Page' ) ) ) :
$data .= '<li class="next-list-item">';
$data .= $next_posts_link;
$data .= '</li>';
endif;
$data .= '</ul>';
endif;
$data .='</nav>';
echo $data;
}
?>
I have added one function at your code please refere this.

Resources