WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] posts_nav_link(); not working (7 posts)

  1. laurencescott
    Member
    Posted 5 months ago #

    Hi Everyone,

    I am completely new to WordPress and I am learning how to develop my own themes.

    I have attempted to create pagination with the posts on my font page but it just displays the same posts on each page.

    Here is my code:

    <?php query_posts("posts_per_page=5"); ?>
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<a class="posttitle" href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    
    		<p><?php the_excerpt(); ?></p>
    
    		<?php endwhile; ?>
    
    		<div id="previousandnext"><p><?php posts_nav_link(); ?></p></div>
    
    		<?php else : ?>
    
    		<h2>Not Found</h2>
    
    		<?php endif; ?>

    Does anyone have any idea what I am doing wrong?

    Thanks in advance :)

  2. WPRanger
    Member
    Posted 5 months ago #

    You need to add the paged parameter to your query:

  3. laurencescott
    Member
    Posted 5 months ago #

    Do you mean like this...?

    <?php query_posts("posts_per_page=5"); ?>
    		<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		query_posts('posts_per_page=5&paged=' . $paged);
    		?>
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<a class="posttitle" href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    
    		<p><?php the_excerpt(); ?></p>
    
    		<?php endwhile; ?>
    
    		<?php posts_nav_link(); ?>
    
    		<?php else : ?>
    
    		<h2>Not Found</h2>
    
    		<?php endif; ?>
  4. alchymyth
    The Sweeper & Moderator
    Posted 5 months ago #

    why do you need the query at all?

    you can set the 'posts per page' from within the dashboard - settings - reading - blog pages show at most...

    if you need/want a different number of posts on the index page than on the rest of the archive pages, review http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Changing_the_number_of_posts_per_page.2C_by_post_type

  5. laurencescott
    Member
    Posted 5 months ago #

    I already have posts per page set up but I would like to add 'newer posts' and 'older posts' links to my blog.

  6. alchymyth
    The Sweeper & Moderator
    Posted 5 months ago #

    that shold work automatically - without the 'query_posts()' code at the start.

    do you have more published posts than set as 'posts per page'?

    review http://codex.wordpress.org/Pagination

  7. laurencescott
    Member
    Posted 5 months ago #

    Thank you everyone. It's solved now.

Reply

You must log in to post.

About this Topic