0

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)."'>&laquo; First</a>";
        if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; 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 &rsaquo;</a>";
        if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
        echo "</div>\n";
    }
}
6
  • Does this answer your question? Pagination when using wp_query? Commented Oct 27, 2022 at 7:19
  • I already tried this. All I got is <div class="pagination"> </div> Commented Oct 27, 2022 at 7:22
  • Can you show the code that you tried that just returned that element? Commented Oct 27, 2022 at 8:22
  • I tried both the solutions submitted by @Dave Romsey mentioned in your link. I removed my code and replaced it with his. Commented Oct 27, 2022 at 8:24
  • 2
    Did you also change the variable that contains the instance of 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 Commented Oct 27, 2022 at 8:41

1 Answer 1

1

You can try this SO question Pagination when using wp_query?

Notice that their example uses the $query variable for the WP_Query instance, while your code uses $the_query.
Also notice that they use paged property in the array that is being passed into WP_Query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.