• Resolved calex3710

    (@calex3710)


    I’m trying to figure out how to create prev/next navigation after a series of the three latest posts — “previous” and “next”. I know it involves the $paged function somehow but I can’t quite figure it out. Right now the next/prev post links are not showing up at all.

    Here’s what I have:

    <?php $counter = 3;
    $recentPosts = new WP_Query();
     $recentPosts->query('showposts=3'.'&paged='.$paged);
    ?>
    
    <?php query_posts('category_name=spotlight'); while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    
    <div id="postsection" class="section">
     <?php if ( has_post_thumbnail()) : ?>
    		<div class="postphoto">
    		     <?php the_post_thumbnail();?>
    		     <div class="slidedate">Aug 2nd, 2011</div>
    		      <div class="slidetitle"><?php the_title(); ?></div>
    		</div>
    		<div class="posttext"><?php the_content(); ?></div>
    		<br class="clear" />
    	       </div>
               	<?php endif; ?>
    		<?php endwhile; ?>
    		<?php previous_posts_link('&laquo; Previous') ?>
    		<?php next_posts_link('More &raquo;') ?>
    		</div>

    Any help would be really appreciated for this PHP noob.

Viewing 14 replies - 1 through 14 (of 14 total)
  • There seem to be multiple problems in the code. Perhaps you need to find a tutor or begin with something less complicated.

    Thread Starter calex3710

    (@calex3710)

    Like I said, I’m learning. The code works just fine except for the next/prev function. Tell me what’s wrong with it and I’ll fix it.

    Here is what I think you need to have for the $paged variable:

    <?php $counter = 3;
    $paged = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
    $recentPosts = new WP_Query();
     $recentPosts->query('showposts=3'.'&paged='.$paged . 'category_name=spotlight');
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

    As for other items:

    • $counter is not used as far as I can tell
    • Your div tags appear to be mismatched. There appears to be an extra closing div inside the if test. Use an editor that will let you indent code to match up opening and closing tags. That will help you isolate problems.
    • This may be what you want, but the code will only show posts that have a thumbnail. If this is not correct, the endif is out of place.
    • The code you showed has an unmatched closing div at the end. This may be OK depending on code that preceeds this.

    If you don’t already have an editor that will let you indent code, try Notepad++.

    Thread Starter calex3710

    (@calex3710)

    Thanks, but I know my way around text editors. The forum messed with my alignments and I was a bit sloppy about not removing some extra divs/PHP code that I was playing around with before I posted (my apologies).

    It’s WordPress/PHP I’m fairly new to. Thanks to your help, my code is currently set up as:

    <?php $paged = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=3'.'&paged='.$paged . 'category_name=spotlight');
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    
    <?php if ( has_post_thumbnail() : ?>
    <div id="postsection" class="section">
    	<div class="postphoto">
    	         <?php the_post_thumbnail());?>
    	         <div class="slidedate">Aug 2nd, 2011</div>
    	         <div class="slidetitle"><?php the_title(); ?></div>
    	</div>
    	<div class="posttext">
    		<?php the_content(); ?>
    	</div>
    	 <br class="clear" />
    </div>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php previous_post_link('%link', 'Prev posts', TRUE); ?>
    <?php next_post_link('%link', 'Next posts', TRUE); ?>

    And BTW, I do want it to only pull in posts with thumbnail photos. This works as before, but I’m still not sure how to bring in previous/next posts as a set of three within the same template structure. Currently the previous_post_link and next_post_link link to one previous and following post, but those posts don’t have their own templates, of course, so they don’t show up when clicked.

    This code appears in the template for a page called ‘spotlight’ (the category of the posts is also ‘spotlight’). What I want is a way to have the previous/next posts show up in a set of three in the same template structure. Does this make sense? Thanks for your help and I apologize for the issues with my post before.

    Now its my turn to apologize – I left out an ampersand in the code I posted before. Please correct the line below by putting one in before category_name:

    $recentPosts->query('showposts=3'.'&paged='.$paged . '&category_name=spotlight');

    And, to get the next set of 3 posts, the function is next_posts_link() instead of next_post_link(). Confusing, huh?

    Thread Starter calex3710

    (@calex3710)

    Thanks. Hmm, previous_posts_link and next_posts_link seem to not show up at all (while previous_post_link and next_post_link do show up but link to the single posts).

    OK – previous/next_posts_link need query_posts() to work, not WP_Query().

    If this is not the primary query on the page, you will need to save the current query and restore it after the call to next/previous_posts_link().

    Thread Starter calex3710

    (@calex3710)

    This is the only query on the page…so do I just replace WP_Query with query_posts?

    Try this:

    <?php $paged = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
    query_posts('showposts=3'.'&paged='.$paged . 'category_name=spotlight');
    while (have_posts()) : the_post(); ?>
    Thread Starter calex3710

    (@calex3710)

    Thanks, the link now shows up! However, because the original page is:www.example.com/spotlight, the next_posts_link calls the link https://www.example.com/spotlight/page/2 … and clicking on it takes me to a “this is embarrassing/page not found” page — which has my header/footer but nothing in the body.

    Is this my page.php file? I tried adding the code into page.php as well but it made no difference. Or does this have something to do with the permalink structure?

    Really appreciate your help.

    It may be the infamous ‘category pagination problem’. Try setting the permalinks to the default temporarily to see if the problem disappears.

    If the problem goes away, you might benefit from the Category pagination fix plugin.

    Also, just as a wild guess, change ‘showposts=3’ to ‘posts_per_page=3’.

    Thread Starter calex3710

    (@calex3710)

    Changing the permalinks to the default worked, so I will try out the plugin to see if I can keep my custom permalinks structure working. Thanks for your help!

    Thread Starter calex3710

    (@calex3710)

    The plugin worked! Seem to be no further issues, so I will close this topic. Thanks again.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘next/prev posts links not working’ is closed to new replies.