0

I have a custom page, where I show the posts from the articles section.

I want to show 6 posts per page, and use a numbered pagination.

Everytime I have to use 'pagination' outside archive.php or something like that, I have problems.

Any ideas on how can I resolve that?

My code:

<?php
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $args = array(
     'post_type' => 'post',
     'posts_per_page' => 6,
     'paged' => $paged,
  );
  $post_query = new WP_Query($args);
  if($post_query->have_posts() ) :
    while($post_query->have_posts() ) :
      $post_query->the_post();
         the_title();
    endwhile;
    if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
    }  
  endif;
  wp_reset_postdata();
?>

My 'functions.php':

(I followed this tutorial: http://callmenick.com/post/custom-wordpress-loop-with-pagination)

function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
    $numpages = 1;
}
}
$pagination_args = array(
'base'            => get_pagenum_link(1) . '%_%',
'format'          => 'page/%#%',
'total'           => $numpages,
'current'         => $paged,
'show_all'        => False,
'end_size'        => 1,
'mid_size'        => $pagerange,
'prev_next'       => True,
'prev_text'       => __('&laquo;'),
'next_text'       => __('&raquo;'),
'type'            => 'plain',
'add_args'        => false,
'add_fragment'    => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
  echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
  echo $paginate_links;
echo "</nav>";
}
}
0

Ok, I found the solution for this specific case, since it's a template for the articles.

On the admin settings, define the custom page as the page for the articles. And using the code I posted in the question, everything works just fine.

Now, if you are using a page for the homepage, another for the articles, and you have another custom page that needs pagination, this solution won't work.

If anyone knows how to fix that, it would be good to leave the info here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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