1

What I am trying to do is display every post from the "News" category (in my case the "news" category's ID is 13), with 5 posts per page. The pagination links display on the page, but when I click on the 'previous' link, it will only show me the first 5 posts in the category.

The contents of page-news.php:

<?php get_header(); ?>
<h1 class="title">Latest News</h1>
<?php
    /*$args = array('cat' => 13);
    $posts = new WP_Query($args);*/
    global $paged;
    global $wp_query;

    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query(array('cat=13', 'paged='.$paged));

    if ($wp_query->have_posts()) :
?>
    <ul id="news">
<?php
        while ($wp_query->have_posts()) : $wp_query->the_post();
?>
        <li>
            <article>
                <h2 class="post-title"><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                <time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?> <?php the_time(); ?></time>
                <?php the_content(); ?>
            </article>
        </li>
<?php
    endwhile;
?>
    </ul>
    <div class="news-nav">
        <?php next_posts_link( __( '&larr; Older posts')); ?>
        <?php previous_posts_link( __( 'Newer posts &rarr;')); ?>
    </div>
<?php
    $wp_query = null;
    $wp_query = $temp;
    else :
?>
<h2>It looks like there's nothing happening at the moment.</h2>
<?php endif; ?>

<?php get_footer(); ?>
1

You're mixing query string and array parameters.

They should either be a single string, like:

$wp_query = new WP_Query( 'cat=13&paged=' . $paged );

or an array of key/value parameters, like:

$wp_query = new WP_Query(
    array(
        'cat' => 13,
        'paged' => $paged
    )
);
4
  • I tried the second one, but I'm still getting the same 5 posts when I click on the previous posts link. – Anna Lam Apr 18 '14 at 4:13
  • See HamzStramGram's answer for properly setting the $paged query var before you use it in the query. – Milo Apr 18 '14 at 4:15
  • It's still not working – Anna Lam Apr 18 '14 at 4:21
  • I had a closer look and the problem was due to the news page being set as the home page. – Anna Lam Apr 18 '14 at 4:37
0

Try to get the paged variable set using this :

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

(And I don't think you need to declare it as a global before)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.