0

I have this theme that I created which is a mix of code copied and mixed from different themes, and I have a custom post page in the theme where pagination isn't working for some reason.

<div class="post-list group">
<?php
  global $layout_design;
  $layout_design  = rwmb_meta( 'layout_select', 'type=select', $page_id);
  $posts = rwmb_meta( 'layout_ppp', 'type=text', $page_id);
  $category_name_page = rwmb_meta( 'layout_category', 'type=text', $page_id);
  $temp = $wp_query; $wp_query= null;
  $wp_query = new WP_Query(); $wp_query->query('showposts='.$posts . '&category_name='.$category_name_page);

  while ($wp_query->have_posts()) : $wp_query->the_post();

  /*
  * Include the post format-specific template for the content. If you want to
  * use this in a child theme, then include a file called called content-___.php
  * (where ___ is the post format) and that will be used instead.
  */

  get_template_part( 'customblog', get_post_format() );
  endwhile;
?>

</div>
<?php
  // Previous/next post navigation.
  pi_pagination('blog');
  else :
    // If no content, include the "No posts found" template.
    get_template_part( 'content', 'none' );
  endif;
?>

</div><!-- #content -->

The pagination code looks like this

if (!function_exists('pi_pagination')) {
  function pi_pagination() {
    global $wp_query, $pi_theme_options, $wp_rewrite;   
    $pages = '';
    $pagination = '';
    $max = $wp_query->max_num_pages;

    // if variable paged isn't set
    if (!$current = get_query_var('paged'))
      $current = 1;

    // set parameters
    $args = array(
      'base' => str_replace(999999999, '%#%', get_pagenum_link(999999999)),
      'format' => '',
      'total' => $max,
      'current' => $current,
      'show_all' => true,
      'type' => 'array',
      'prev_text' => '&larr;',
      'next_text' => '&rarr;',
      'prev_next' => false,
      'mid_size' => 3,
      'end_size' => 1
    );

    $previous_label = __('&#60; Previous', 'pi_framework');
    $next_label = __('Next &#62;', 'pi_framework');

    // previous and next links html
    $prev_page_link = $current == 1 ? "" : "<li><a class='icon-double-angle-left' href='" . get_pagenum_link($current - 1) . "'><i class='fa fa-arrow-left'></i></a></li>";
    $next_page_link = $current == $max ? "" : "<li><a class='icon-double-angle-right' href='" . get_pagenum_link($current + 1) . "'><i class='fa fa-arrow-right'></i></a></li>";

    // get page link
    $pagination_links = paginate_links($args);

    // get page indicator format from theme options
    $page_indicator = $pi_theme_options['pagination'];
    $page_indicator = str_replace('%current%', '%1$d', $page_indicator);
    $page_indicator = str_replace('%total%', '%2$d', $page_indicator);
    $page_indicator = sprintf($page_indicator, $current, $max);

    // loop through pages
    if (!empty($pagination_links)) {
      foreach ($pagination_links as $index => $link) {
        $link = str_replace('</span>', '</a>', $link);
        $link = str_replace('<span', '<a', $link);

        $pagination .= "<li " . (($index + 1) == $current ? "class='active'>" : ">") . $link . "</li>";
      }
    }

    // if there is more then one page send html to browser
    if ($max > 1) {
      $container = 'li';

      echo "<{$container} class='pagination'>
        <ul>
          <li class='current-page'>
            <span>{$page_indicator}</span>
          </li>
          {$prev_page_link}
          {$pagination}
          {$next_page_link}
        </ul>
      </{$container}>";
    }
  } 
}

Everything seems to be OK with the way I wrote the query but for some reason clicking the pagination links doesn't give me the next set of links.

  • your query doesn't set any pagination argument, you ask for the first page of posts regardless of page number. Look up WP_Query in the documentation and read about pagination. – Milo Feb 15 '15 at 22:27
  • '&category_name='.$category_name_page. Does this mean this is in category.php?. Why are you running a custom query if it is. Please see this post. Also, don't nullify the main query – Pieter Goosen Feb 16 '15 at 4:32

Your Answer

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

Browse other questions tagged or ask your own question.