Forums

creating "Next Page" or "Older Posts" navigation (7 posts)

  1. mrwolfy
    Member
    Posted 2 years ago #

    I'm using the "A page of posts" to crate a sort of blog page.

    My question is how do I limit the number of posts, but also allow navigation to the next oldest posts.

    I am easily able to limit the number of posts, but I have not figured out how to add "Next Page" / "Previous Page" links.

    Here is my page in progress.

    below is the code:

    <?php
    // page id 21 will get category ID 12 posts, page 16 will get category 32 posts, page 28 will get category 17 posts
    if (is_page('76') ) {
    $cat = array(4);
    } elseif ( is_page('16') ) {
    $cat = array(32);
    } elseif ( is_page('28') ) {
    $cat = array(4);
    } else {
    $cat = '';
    }
    
    $showposts = -1; // -1 shows all posts
    $do_not_show_stickies = 1; // 0 to show stickies
    $args=array(
       'category__in' => $cat,
       'showposts' => $showposts,
       'caller_get_posts' => $do_not_show_stickies
       );
    $my_query = new WP_Query($args); 
    
    ?>
    
    	<?php if( $my_query->have_posts() ) : ?>
    
    		<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    			<?php
    			//necessary to show the tags
    			global $wp_query;
    			$wp_query->in_the_loop = true;
    			?>
    
    				<div class="entry">
    					<?php the_content('Read the rest of this entry ยป'); ?>
    				</div>
    
    				<p class="postmetadata"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> | </a> Posted <?php the_time('F jS, Y') ?> | Category "<?php the_category(', ') ?>" <?php edit_post_link('| Edit','',''); ?> </p>
    
    		<?php endwhile; ?>
    
    	<?php posts_nav_link(); ?>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
  2. miocene22
    Member
    Posted 2 years ago #

    <div class="pnavigation">
    		<p class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?>
    		</p>
    		<p class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?>
    		</p>
    	</div>

    But this between <?php endwhile; ?> and <?php else : ?>

    it'll only show if there are more posts existing than the number of posts displayed.
    I.e if you have it set to display 10 posts but only 7 exists, the links will not show

  3. mrwolfy
    Member
    Posted 2 years ago #

    Thanks for the answer! But for some reason it did not work.

    I have two posts, I set my "$showposts" var to 1 , but there are no links.

    I tried a few things... Still nothing.

  4. miocene22
    Member
    Posted 2 years ago #

    your loop is a bit more sophisticated than I have used in the past...

    This is my entire loop where the next and previous post links do work:

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    	<div class="entry">
    		<div class="posthead">
    			<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    			</h3>
    			<div class="dater">
    				<?php the_time('l, F j, Y'); ?></div>
    		</div>
    		<div class="entry">
    			<?php the_content('Read on &raquo;'); ?></div>
    	</div>
    	<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?>Posted in
    	<?php the_category(', ') ?>| <?php edit_post_link('Edit', '', ' | '); ?>
    	<span class="comments"><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    	</span></p>
    	<?php endwhile; ?>
    	<div class="pnavigation">
    		<p class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?>
    		</p>
    		<p class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?>
    		</p>
    	</div>
    	<?php else : ?>
    	<h4 class="center">Not Found</h4>
    	<p class="center">Sorry, but you are looking for something that isn't here.</p>
    	<?php endif; ?>

    I know it doesn't have the initial arguments that your loop has but the main structure is the same - not sure why your isn't working

  5. mrwolfy
    Member
    Posted 2 years ago #

    Ok, thanks for your time. I am no php expert, I pasted that loop from the codex. It does pretty much what I need except for display links to older posts.

    I tried your loop, the results are strange for me, I don't know why, but it seems not to display my posts properly, but an excerpt or a link to the page itself, like I said, strange.

    Best,

    Wolfy

  6. miocene22
    Member
    Posted 2 years ago #

    it seems not to display my posts properly

    I would expect that is because your stylesheet is not set up to style the div classes that my loop generates like posthead, dater etc. And the fact that the "entry" div is in a different postion.

  7. mrwolfy
    Member
    Posted 2 years ago #

    yes my style sheet is a bit of a mess! I will sort it out and give it another try. Thanks.

    W

Topic Closed

This topic has been closed to new replies.

About this Topic