I’m looking to add a “Next” and “Previous” links to a custom blog page I’m building in WordPress. What I have setup is a custom grid from Bootstrap, but I want to limit this grid to 12 posts per page (for obvious optimisation reasons). I’m a front-end developer so it’s a bit tricky for me to work out the PHP back-end.
How can I add a query in my blog page to include the next and previous links? I’m tried quite a few different solutions but can’t seem to get it working with my loop.
Thanks!
Code below:
<?php
/*
Template Name: Blog Page
*/
?>
<?php get_header(); ?>
<header class="entry-header">
<h1 class="entry-title"><?php echo get_the_title(); ?></h1>
</header>
<div class="container">
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
if($i % 3 == 0) { ?>
<div class="row">
<?php
}
?>
<div class="col-md-4">
<h4><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h4>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(200,150) ); ?></a>
<?php the_excerpt(); ?>
</div>
<?php $i++;
if($i != 0 && $i % 3 == 0) { ?>
</div><!--/.row-->
<div class="clearfix"></div>
<?php
} ?>
<?php
endwhile;
}
wp_reset_query();
?>
</div><!--/.container-->
<?php get_footer(); ?>
Use the
paginate_links
hook in combination with a customWP_Query
array. Make sure to specify thepaged
array parameter for the query. This sets up the query to give back paged results.This is quite a common issue – you may find all the info here https://codex.wordpress.org/Function_Reference/paginate_links
there are some examples also you may use right a way