WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination on front-page.php (5 posts)

  1. phpchick
    Member
    Posted 1 year ago #

    I cannot seem to figure out what I am doing wrong.

    I simply want to add pagination to my home-page. The person who built the theme has the wp-paginate plugin installed but I can't seem to get that to work either so I've been trying to handcode the pagination in and have been experiencing difficulty.

    Here is the actual page http://goo.gl/A0LRu

    I'm not getting even links at the bottom that say "next" or "Previous"

    <?php
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    		$args = array(
    			'post_type' => 'earnings',  //use to be stock
    			'posts_per_page' => get_field('stocks'), //use to be stocks
    			'paged' => $paged
    		);
    		$the_query = new WP_Query( $args );
    		while ( $the_query->have_posts() ) : $the_query->the_post();
    		?>
    
    <!-- THE MEAT OF THE WHILE LOOP THAT DISPLAYS CONTENT -->
    
    	<?php endwhile; ?>
    
    <div class="navigation">
    	<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
    	<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
    		</div>
    
    	<?php if(function_exists('wp_paginate')) {  //THIS IS ME PLAYING AROUND WITH wp_paginate but not succeeding
    	    wp_paginate();
    	} ?>
    
     <?php wp_reset_postdata(); ?>
  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    try:

    <div class="navigation">
    	<div class="alignleft"><?php next_posts_link('&laquo; Older Entries',$the_query->max_num_pages) ?></div>
    	<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;',$the_query->max_num_pages) ?></div>
    		</div>

    http://wordpress.stackexchange.com/questions/20424/wp-query-and-next-posts-link

  3. phpchick
    Member
    Posted 1 year ago #

    So using that snippet causes at least the link "older entries" to show up.

    When you click on it however, it goes to url.com/page/2/ but all the stories are the same as the first.

    In addition, the link on 'page/2' is a link to itself, 'page/2' if that makes sense.

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    there might be something before your code (could be in any template which gets included or called before your code, or from a plugin) which messes with the querystring, so that the 'paged' information is lost.

    make sure all queries are closed off properly with wp_reset_query() or wp_reset_postdata().

    for debugging, try to print the paged variable to see if it gets set properly.

  5. phpchick
    Member
    Posted 1 year ago #

    Just want to follow up. I solved the problem and I did it by adding these lines

    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'];
    }

    I added them right before these lines

    $wp_query = new WP_Query( $args );    //these 3 $wp_query use to be "$the_query"   --  when you change these to wp_query, it seems to enable the wp_paginate
    		while ( $wp_query->have_posts() ) : $wp_query->the_post();

    and the reason is this

    "$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."

    And I found the above (and the solution), here http://wpquestions.com/question/show/id/701

    And then, to get wp_paginate working properly (it was showing every post on the same page, while looping the html over and over again),

    I added wp_reset_query immediately following the code for wp-paginate, like so

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

    Thanks for all the help alchymyth x0x0

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags