WordPress.org

Ready to get started?Download WordPress

Forums

get_query_var('paged') doesn't seem to work on page templates (12 posts)

  1. philipwalton
    Member
    Posted 4 years ago #

    I'm have my site set to display a page instead of recent posts as the front page of my blog. That page is using a template I've created to show all posts that are in the category of the most recent post.

    This is all fine and good until I need to use pagination, which doesn't seem to work on 'pages'

    I'm using the method of:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=$recent&paged=$paged");

    But this isn't working. To test, I simply echo get_query_var('paged') and all I get is an empty string.

    I know this is an issue specifically with page templates because if I copy my exact code to the index.php file, it works fine.

    Is there something that prevents pagination on pages? Is there another way I can get the current page without using get_query_var('paged')?

  2. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    i use this method in page templates all the time, without any problems.

    try an alternative way of writing this:
    query_posts("cat=$recent&paged=$paged");

    this way:
    query_posts('cat=$recent&paged=' . $paged);

    if that does not work, it might also be useful to see the full code of your page template;
    you could paste it into a http://wordpress.pastebin.com/ and post the link to it here.

  3. philipwalton
    Member
    Posted 4 years ago #

    alchymyth, thanks for the response; however, the problem is not with my syntax. The problem is that the get_query_var('paged') function does not seem to work for pagination on custom page templates.

    If I put get_query_var('paged') in my index.php file and I enter the URL: domain.com/page/2, that function will return 2. However, if I put that code on a page template called archives, and I enter the URL: domain.com/archives/page/2, it returns an empty string.

    Also, does WordPress clear the $_GET variables after it uses them? If I do print_r($_GET), it returns and empty array. That 2 in the URL must be getting stored somewhere, and if I could figured out where, I can make this function work.

    At minimum I could get the value from the URL itself, but that isn't a full proof solution.

    Any help would be much appreciated.

  4. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    if that does not work, it might also be useful to see the full code of your page template;
    you could paste it into a http://wordpress.pastebin.com/ and post the link to it here.

    also: do you do any custom query before you do the 'pages' stuff?

    if so, try to add wp_reset_query(); before this get_query_var('paged'); code.

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

  5. philipwalton
    Member
    Posted 4 years ago #

    I case anyone else is having this same problem, get_query_var('page') seems to work on pages the same way get_query_var('paged') works on the index page. (Notice 'page' vs. 'paged')

    I discovered this by doing print_r($wp_query->query_vars); on both and I realized that the results were quite different, even with the exact same code in the templates.

    WordPress must treat the URL variables very differently when it's on a page. Personally this doesn't make much sense to me because if you want several different options for displaying all your posts, the best way to do that is with different page templates.

    If anyone can clarify this for me, that would be great. At this point it doesn't seem to make much sense.

  6. philipwalton
    Member
    Posted 4 years ago #

    I should also clarify that this is refering only to static pages being set as the home page. I went back and tried the code on a static page NOT set as the home page and get_query_var('paged') worked fine.

    Using get_query_var('page') seems to only be necessary if you're using query posts in a custom page template that you've set as your homepage.

  7. Devin Walker
    Member
    Posted 3 years ago #

    What alchymyth said was correct; Most likely you have need to use wp_reset_query to get the pagenation to work properly.

    Here's my working code:

    <?php
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$args = array (
    			'category_name' => $theCat,
    			'orderby' => 'date',
    			'posts_per_page' => '12'
    
    			); query_posts($args . '&paged=' . $paged); ?>
    
    			 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
    				$thumb=get_post_meta($post->ID, 'press-thumb', true);
                    $thumb=get_post_meta($post->ID, 'gallery-slide-direction', true);
    				?> 
    
     				 <li style="display: block;" class="<?php echo $post_category[0]->category_nicename ?> boxgrid <?php echo get_post_meta($post->ID, "gallery-slide-direction", $single = true); ?>">
    
                    	<div class="cover">
    					<?php the_date('F Y', '<h4 class="pressDate">' , '</h4>'); ?>
                        <h3><?php the_title(); ?></h3>
                        <?php the_excerpt(); ?> 
    
                        <a href="<?php the_permalink(); ?>" class="opener">Preview this &raquo;</a>
                        </div>
    
                        <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "press-thumb", $single = true); ?>&h=160&w=240&q=100&zc=1" alt="<?php the_title(); ?>" width="240" height="160" />
    
                      </li>
    
    				 <?php endwhile; endif; ?>
    
                 </ul>
                 <?php if(function_exists('wp_pagenavi')) :  wp_pagenavi();  endif; wp_reset_query();?>
  8. ericfelker
    Member
    Posted 3 years ago #

    Thanks so much for this! I was puzzled as to what had skewed my pagination, but you provided the answer. Cheers!

  9. zxdong262
    Member
    Posted 3 years ago #

    philipwalton is right!when static page being set as the home page. use
    get_query_var('page')
    it works!thanks,i search the answer for a while.

  10. DDT
    Member
    Posted 3 years ago #

    hmmm i can't get it to work.

    I am using a category index like this

    $post = $wp_query->post;
      if (in_category('news')) {
          include(TEMPLATEPATH.'/category_news.php');
      } elseif(in_category('board')) {
          include(TEMPLATEPATH.'/category_board.php');
      }
      else{
          include(TEMPLATEPATH.'/category_default.php');
      }

    mydomain.com/news/ shows me the last 5 news posts in category news but now mydomain.com/news/page/2 doesn't work it just sredirect to the first page.

    In category_news.php i have

    //$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$paged = (get_query_var('page')) ? get_query_var('page') : 1;
    		$args = array (
    			'category_name' =>'news' ,
    			'posts_per_page' => 5,
    			'paged'=>$paged
    			); 
    
    		query_posts($args);

    How can i point news/page/2 to the category_news.php file??

  11. taina928
    Member
    Posted 3 years ago #

    Can someone help me?
    How do I implement pagination here:

    <?php
    $tgs = array();
    $tgs[] = get_query_var('s');
    $tags = implode(",",$tgs);
    query_posts('tag='.$tags);
    ?>

    Thanks!

  12. pepperlime
    Member
    Posted 3 years ago #

    In case anyone else is having this same problem, get_query_var('page') seems to work on pages the same way get_query_var('paged') works on the index page. (Notice 'page' vs. 'paged')

    Thank you SO much! I have been searching for a solution to this for 2 days, doing all sorts of crazy fixes and tearing my hair out over my pagination not working.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.