logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Pagination Not Working on Front Page

Hello!
I have a small annoying issue here with pagination. I think I'm just overlooking something simple.

My theme has a homepage template and a blog page template. Sometimes users want to use the blog page as the homepage instead.

When users set that blog template as the front page (Settings » Reading » Static Page as.. » Blog) the pagination stops working. When you click the Next Page button, it goes to the next page, but shows the same posts as the first page. When the blog page is NOT used as the front page, the pagination works fine. Very odd.

This is the contents of my blog page:


<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=5&cat='.get_option('prototype_blog_cat'));
?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<div class="post">

// post content here //

</div><!--end post-->

<?php endwhile; ?>

<div class="navigation">
<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>

<?php $wp_query = null; $wp_query = $temp;?>


Look forward to hearing your answers!
Mike

This question has been answered.

Mike McAlister | 08/02/10 at 1:18am Edit


(5) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    08/02/10
    3:01am
    Utkarsh Kukreti says:

    Try adding this as the first line

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    • 08/02/10 1:23am

      Mike McAlister says:

      Where exactly, Utkarsh?

    • 08/02/10 1:24am

      Utkarsh Kukreti says:

      Before

      $temp = $wp_query;

    • 08/02/10 1:27am

      Mike McAlister says:

      Hmm, that one didn't work. Didn't seem to have any effect.

    • 08/02/10 1:30am

      Utkarsh Kukreti says:

      Could you link me to the site? (or message me from my profile here)

  • avatar
    Last edited:
    08/02/10
    1:27am
    Rashad Aliyev says:

    Other solutions: You can use the famous WP-PageNavi plugin.

    http://wordpress.org/extend/plugins/wp-pagenavi/

    best regards,

    • 08/02/10 1:29am

      Mike McAlister says:

      I did try that previously, but that was also not working properly. I don't want to rely on a plugin though. I would like this to work out of the box.

    • 08/02/10 1:35am

      Rashad Aliyev says:

      Install it WP-PageNavi plugin


      use the query like this.

      That mean: not showing cat id:1 , and shows 5 posts.


      <?php query_posts("cat=-1&showposts=5&paged=$paged");?>




      then add this code where you want.

      It'll show your navigation truly.
      <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>


      Still you've problem or questions, feel free to contact.

    • 08/02/10 1:38am

      Mike McAlister says:

      Maybe if all else fails I will try the plugin again. But like I said, it's very important that the theme works out of the box, without that plugin.

    • 08/02/10 1:39am

      Rashad Aliyev says:

      Install PageNavi plugin and change this check it again.



      <div class="navigation">

      <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

      </div>


      and also I'll try to solve it without plugins.

    • 08/02/10 2:07am

      Rashad Aliyev says:


      Use this codes istead of yours.

      It's worked with WP-PageNavi plugin very well!






      <?php

      $temp = $wp_query;

      $wp_query= null;

      $wp_query = new WP_Query('paged='.$paged.'&showposts=5&cat='.get_option('prototype_blog_cat'));
      while ($wp_query->have_posts()) : $wp_query->the_post();
      ?>


      <div class="post">



      // post content here //



      </div><!--end post-->

      <?php
      endwhile;
      if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
      $wp_query = null; $wp_query = $temp; ?>



    • 08/02/10 2:22am

      Rashad Aliyev says:

      For using defaul pagination read this references;

      http://codex.wordpress.org/Function_Reference/get_query_var

  • avatar
    Last edited:
    08/02/10
    2:01am
    Chris Olbekson says:

    Here is a very reliable numeric pagination function you can add to functions.php. Then in your templates just add this line right after

    <?php endwhile; ?>


    <?php numeric_pagination(); ?>


    For the blog template add this right before the loop:
    <?php
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("showposts=6&paged=$page");
    ?>



    Here is the numeric pagination function:

    /* Numeric Pagination ********************************************/

    function numeric_pagination ($pageCount = 9, $query = null) {

    if ($query == null) {
    global $wp_query;
    $query = $wp_query;
    }

    if ($query->max_num_pages <= 1) {
    return;
    }

    $pageStart = 1;
    $paged = $query->query_vars['paged'];

    // set current page if on the first page
    if ($paged == null) {
    $paged = 1;
    }

    // work out if page start is halfway through the current visible pages and if so move it accordingly
    if ($paged > floor($pageCount / 2)) {
    $pageStart = $paged - floor($pageCount / 2);
    }

    if ($pageStart < 1) {
    $pageStart = 1;
    }

    // make sure page start is
    if ($pageStart + $pageCount > $query->max_num_pages) {
    $pageCount = $query->max_num_pages - $pageStart;
    }

    ?>
    <div id="archive_pagination">
    <?php
    if ($paged != 1) {
    ?>
    <a href="<?php echo get_pagenum_link(1); ?>" class="numbered page-number-first"><span>&lsaquo; <?php _e('Newest', 'theme'); ?></span></a>
    <?php
    }
    // first page is not visible...
    if ($pageStart > 1) {
    //echo 'previous';
    }
    for ($p = $pageStart; $p <= $pageStart + $pageCount; $p ++) {
    if ($p == $paged) {
    ?>
    <span class="numbered page-number-<?php echo $p; ?> current-numeric-page"><?php echo $p; ?></span>
    <?php } else { ?>
    <a href="<?php echo get_pagenum_link($p); ?>" class="numbered page-number-<?php echo $p; ?>"><span><?php echo $p; ?></span></a>

    <?php
    }
    }
    // last page is not visible
    if ($pageStart + $pageCount < $query->max_num_pages) {
    //echo "last";
    }
    if ($paged != $query->max_num_pages) {
    ?>
    <a href="<?php echo get_pagenum_link($query->max_num_pages); ?>" class="numbered page-number-last"><span><?php _e('Oldest', 'theme'); ?> &rsaquo;</span></a>
    <?php } ?>

    </div>

    <?php } ?>

    Previous versions of this answer: 08/02/10 at 2:01am

  • avatar
    Last edited:
    08/02/10
    1:45am
    juan manuel incaurgarat says:

    try putting this just before $wp_query->have_posts(), consider putting the query on a variable after the while loop:


    <?php $page = (get_query_var(’paged’)) ? get_query_var(’paged’) : 1;
    query_posts(”showposts=4&paged=$page”); ?>

    Previous versions of this answer: 08/02/10 at 1:45am

  • avatar
    Last edited:
    08/02/10
    2:26am
    wjm says:

    i tracked down the problem, and i am not sure whethere this is a wordpress bug.
    $paged (it doesnt matter if you make it global and access via the variable $paged or you use get_query_var('paged') always resolves to 0.
    The /page/2 matches the correct rewrite rules.

    matched_rule: page/?([0-9]{1,})/?$
    matched_query: index.php?&paged=$matches[1]
    matched_query (evaluated): &paged=2

    but, $paged is 0 no matter what.
    trying to track down where there wordpress bug could be.
    but i have the feeling its a wordpress bug.

    • 08/02/10 2:36am

      wjm says:

      It has to do with this piece of code.
      in wp-includes/query.php


      // Correct is_* for page_on_front and page_for_posts
      if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
      $_query = wp_parse_args($query);
      if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
      $this->is_page = true;
      $this->is_home = false;
      $qv['page_id'] = get_option('page_on_front');
      // Correct <!--nextpage--> for page_on_front
      if ( !empty($qv['paged']) ) {
      $qv['page'] = $qv['paged'];
      unset($qv['paged']);
      }
      }
      }


      for some reason wordpress is "correcting" following pages for front pages.
      if you comment this line
      //unset($qv['paged']);
      your code is likely to work

    • 08/02/10 2:39am

      Mike McAlister says:

      Hi wjm,
      This may work, but since it's a theme I am distributing, I can't very well ask that all buyers go into their wp-includes folder to make edits.

    • 08/02/10 2:51am

      wjm says:

      you can fix it by hacking the paged query var.

      add this at the beggning of your code


      global $paged, $wp_query, $wp;
      $args = wp_parse_args($wp->matched_query);
      if ( !empty ( $args['paged'] ) ) {
      $wp_query->set('paged', $args['paged']);
      $paged = $args['paged'];
      }


      that solves the issue.

    • 08/02/10 3:03am

      wjm says:

      this is an improved version.


      <?php
      global $paged, $wp_query, $wp;
      $args = wp_parse_args($wp->matched_query);
      if ( !empty ( $args['paged'] ) && 0 == $paged ) {
      $wp_query->set('paged', $args['paged']);
      $paged = $args['paged'];
      }

      $temp = $wp_query;

      $wp_query= null;

      $wp_query = new WP_Query();

      $wp_query->query('paged='.$paged.'&showposts=5&cat='.get_option('prototype_blog_cat'));

      ?>



      <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>



      <div class="post">



      // post content here //



      </div><!--end post-->



      <?php endwhile; ?>



      <div class="navigation">

      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>

      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>

      </div>



      <?php $wp_query = null; $wp_query = $temp;?>


This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.