WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] previous_posts_link and next_posts_link do not appear in custom query (5 posts)

  1. echofoxtrot
    Member
    Posted 1 year ago #

    I have the following code in my site. The navigation links do not appear; I only see empty div tags.

    What am I doing incorrectly? Thanks in advance for any help you can provide.

    [please mark any posted code - http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    $temp = $wp_query;
    $wp_query = null;
    $paged = (get_query_var ('paged') ) ? get_query_var ('paged') : 1;
    $priorPosts = new WP_Query('offset=2&cat=-37&paged=' .$paged);
    while($priorPosts->have_posts() ):
    $priorPosts->the_post();
    ?>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php endwhile; ?>
    
    <div class="paging">
    <div class="right"><?php previous_posts_link(); ?></div>
    <div class="left"><?php next_posts_link (); ?></div>
    </div>
    <?php
    $wp_query = null;
    $wp_query = $temp;
    ?>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    Try changing these lines:

    $temp = $wp_query;
    $wp_query = null;
    $paged = (get_query_var ('paged') ) ? get_query_var ('paged') : 1;
    $priorPosts = new WP_Query('offset=2&cat=-37&paged=' .$paged);
    while($priorPosts->have_posts() ):
    $priorPosts->the_post();

    to this:

    $temp = clone $wp_query;
    $wp_query = null;
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    query_posts('offset=2&cat=-37&paged=' .$paged);
    while( have_posts() ):
       the_post();

    and this:

    $wp_query = $temp;

    to this:

    $wp_query = clone $temp;
  3. echofoxtrot
    Member
    Posted 1 year ago #

    Thanks, vtxyzzy! I will give that a shot. Much appreciated!

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    you have actually done fine with using WP_Query() - http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1

    for the pagination try:

    <div class="right"><?php previous_posts_link('&laquo; Previous Page', $priorPosts->max_num_pages ); ?></div>
    <div class="left"><?php next_posts_link ('Next Page &raquo;', $priorPosts->max_num_pages ); ?></div>

    however, with the 'offset' parameter in the query, pagination will not work. you will need to find a different way of excluding the two first posts.

  5. echofoxtrot
    Member
    Posted 1 year ago #

    Hi, alchymyth. Thanks - that answers my question. Sorry to take so long to respond to your answer. Glad to know that it is not just some goof I made. I can probably do this by changing the categories on those.

Topic Closed

This topic has been closed to new replies.

About this Topic