I created an archive-sticky.php where I added the code below to show all sticky posts in one single page with pagination as there will be 100s of sticky posts in the coming days.
Now, this code displays the content as it should but the pagination is not showing/working. Where am I going wrong? I have read up 10s of queries here but nothing seems to solve my problem.
The pagination is working for other pages though.
<?php get_header();
/*
Template Name: Archives Sticky
*/
?>
<?php
$the_query = new WP_Query( array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
));
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Sticky Featured posts</h2>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</div>
<?php endwhile; ?>
<?php if (function_exists("pagination")) {
pagination($custom_query->max_num_pages);
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
</div>
<?php get_footer(); ?>
Here's how the functions.php looks like:
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\"><span>Page ".$paged." of ".$pages."</span>";
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>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
<div class="pagination"> </div>
WP_Query
? you have a variable named$the_query
, they have$query
. Also notice that they use paged property in the array that is being passed into WP_Query