Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

pagination not working with query_posts? WordPress

  • SOLVED

On a site I'm working on I use the code below for index.php, but by some reason I can't get the pagination working. When clicking "older posts" I am sent to a /page/2/, but its only containing the same posts as the first page.


<?php

get_header(); ?>

<div id="container">
<div id="content" role="main">
<?php query_posts('posts_per_page=7&orderby=date&order=DESC&cat=5'); ?>
<?php if (have_posts()) : ?>
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<?php if ($i == 1) { ?>
<div class="news-large clearfix">
<div class="news-large-image">
<a href="<?php the_permalink();?>"><?php the_post_thumbnail('news-big','class=alignleft wp-post-image size-medium'); ?></a>
</div>
<div class="news-large-text">
<div class="news-large-title">
<a href="<?php the_permalink();?>"><h1><?php the_title(); ?></h1></a>
</div>
<div class="entry-meta">
<?php the_time('l j. F, Y'); ?>
</div><!-- .entry-meta -->

<div class="news-large-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div><!-- news-large -->
<?php } ?>
<?php if ($i == 2) { ?>
<div class="news-small-container clearfix">
<div class="news-small left">
<div class="news-small-image">
<a href="<?php the_permalink();?>"><?php the_post_thumbnail('news-small'); ?></a>
</div>
<div class="news-small-text">
<div class="news-small-title">
<a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
</div>
<div class="entry-meta">
<?php the_time('l j. F, Y'); ?>
</div><!-- .entry-meta -->
<div class="news-small-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php } ?>
<?php if ($i == 3) { ?>
<div class="news-small right">
<div class="news-small-image">
<a href="<?php the_permalink();?>"><?php the_post_thumbnail('news-small'); ?></a>
</div>
<div class="news-small-text">
<div class="news-small-title">
<a href="<?php the_permalink();?>"><h2><?php the_title(); ?></h2></a>
</div>
<div class="entry-meta">
<?php the_time('l j. F, Y'); ?>
</div><!-- .entry-meta -->
<div class="news-small-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php } ?>
<?php if ($i == 3) { $i = 1; } ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Eldre innlegg', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Nyere innlegg <span class="meta-nav">→</span>', 'twentyten' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?>

</div><!-- #content -->
<?php get_sidebar(); ?>

</div><!-- #container -->
<?php get_footer(); ?>


The strange thing is - if i use a WP_Query with the same arguments in an array instead of the query_posts, like this...


<?php
$args = array(
'posts_per_page' => '7',
'orderby' => 'date',
'order' => 'DESC',
'cat' => '5'
);
$the_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<?php $i = 0; ?>
...and so on


the pagination works, but all the arguments are ignored?!? Also - that would probably be the "wrong" use of a "new WP_Query()" ?

Could anyone help fixing this? I also got another small problem in the code here, but this is the most annoying part, so I'll leave the other problem for a seperate question, just in case anyone spots it...

Answers (3)

2011-03-06

Andrzej answers:

add this before query_posts:

global $paged;

and change query_posts to:

query_posts('posts_per_page=7&orderby=date&order=DESC&cat=5&paged='.$paged);

should do.


Andrzej comments:

in case it won't work, instead of global $paged, try:

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

2011-03-06

Utkarsh Kukreti answers:

Change

<?php query_posts('posts_per_page=7&orderby=date&order=DESC&cat=5'); ?>

to
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
<?php query_posts('posts_per_page=7&orderby=date&order=DESC&cat=5&paged='.$paged); ?>

2011-03-06

Oleg Butuzov answers:

this stuff dosn't work in this way...
read this answer for more explanations

http://wpquestions.com/question/show/id/1762


Oleg Butuzov comments:

And yeah, gettting paged variable will help you... but temporary.
you will get a wrong number of pages...