Next and Previous Posts
-
I have a blog and i want my template to only show one post(done) and displays links(done) to the next and previous posts.
Also how would I check for oldest and newest.
So to be clear here is my questions:
1) How do I get the links for the next and previous posts of the current post?
2) how do i check for the oldest and newest post?
-
Not quite sure what the proper answer should be.
1) If you are wanting to paginate after a query_posts loop, you must include the ‘paged=’ argument in the call to query_posts and then use the previous/next_posts_link() functions to get the links.
2) Do you want to display the posts, or just control the previous/next links? If you want to display the posts, you will need queries to search for the max or min of post_date. If you want to control the pagination, that is built in to previous/next_posts_link().
When you click the previous or next link is displays the next or previous post in place of the current one
That is exactly what my answer to 1) above is intended to do.
1) If you are wanting to paginate after a query_posts loop, you must include the ‘paged=’ argument in the call to query_posts and then use the previous/next_posts_link() functions to get the links.
I can’t be more specific without seeing the code in your template. If you are using a free theme, post a link to a page that shows the problem so I can take a look.
If you are using a paid theme, contact the theme supplier.
is this what im looking for?
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args=array( 'cat'=>3, 'caller_get_posts'=>1, 'paged'=>$paged, ); query_posts($args); ?>
That looks correct to me. Is it not working?
You didn’t show the code for the previous/next_posts_link calls, so the problem might not be obvious.
Heres all the code for the post
<?php if ( have_posts() ) : the_post(); ?> <!-- The following tests if the current post is in category 3. --> <!-- If it is, the div box is given the CSS class "post-cat-three". --> <!-- Otherwise, the div box will be given the CSS class "post". --> <?php if ( in_category('3') ) { ?> <div class="post-cat-three"> <?php } else { ?> <div class="post"> <?php } ?> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $sticky=get_option('sticky_posts'); $args=array( 'cat'=>3, 'caller_get_posts'=>1, 'post__not_in' => $sticky, 'paged'=>$paged, ); query_posts($args); ?> <!-- Display the Title as a link to the Post's permalink. --> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/latestnews.png" style="margin-top:5px; margin-left:0;"/></a></h2> <small>Author: <?php the_author_posts_link(); ?></small> <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. --> <div class="datebg"> <span class="date"> <?php the_time('F j, Y') ?> </span> </div> <!-- Display the Post's Content in a div box. --> <div class="entry"> <?php $entry = the_content(); $limit = 200; substr($entry,0,$limit); ?> <table class="postnav" style="margin-top:5px;"> <tr> <td><a href="<?php get_next_post() ?>"><img src="<?php bloginfo('template_url'); ?>/images/newernone.png" class="newer" /></a></td> <td><a href="#"><img src="<?php bloginfo('template_url'); ?>/images/readmore.png" class="readmore" /></a></td> <td><a href="<?php get_adjacent_post(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/older.png" class="older" /></a></td> </tr> </table> </div> </div> <!-- closes the first div box --> <!-- Stop The Loop (but note the "else:" - see next line). --> <?php else: ?> <!-- The very first "if" tested to see if there were any Posts to --> <!-- display. This "else" part tells what do if there weren't any. --> <p>Sorry, no posts matched your criteria.</p> <!-- REALLY stop The Loop. --> <?php endif; ?>
<table class="postnav" style="margin-top:5px;"> <tr> <td><a href="<?php get_next_post() ?>"><img src="<?php bloginfo('template_url'); ?>/images/newernone.png" class="newer" /></a></td> <td><a href="#"><img src="<?php bloginfo('template_url'); ?>/images/readmore.png" class="readmore" /></a></td> <td><a href="<?php get_adjacent_post(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/older.png" class="older" /></a></td> </tr> </table>
there are a few things not working in your code:
get_next_post()
does not output the url of the next post;
get_adjacent_post()
does also not output the url of the adjacent post.see:
https://codex.www.ads-software.com/Function_Reference/get_next_post
https://codex.www.ads-software.com/Function_Reference/get_adjacent_postto get these links, you would probably use:
next_posts_link()
andprevious_posts_link()
https://codex.www.ads-software.com/Function_Reference/next_posts_link
https://codex.www.ads-software.com/Function_Reference/previous_posts_linkthe check, if there are next or prev posts, is best done with a conditonal statement using ‘get_next_posts_link()’
a possible way of re-writing your code:
https://wordpress.pastebin.com/mi8pMStzbefore editing your theme files, make a backup copy
its not working, both next and previous buttons are faded out even though i have 5 posts made
re-work the structure of your loop:
https://codex.www.ads-software.com/The_Loop_in_Action
imho, your code is missing a
while(have_posts())
andquery_posts()
is coming too late in the code; it should be either before or just after theif(have_posts())
but isnt that going to make it display more than one post at a time?
query_posts()
posts_per_page
https://codex.www.ads-software.com/Function_Reference/query_posts#Post_.26_Page_Parameters
This is what i have now but the buttons still dont work.
<?php if ( have_posts() ) : the_post(); ?> <?php while (have_posts()) : the_post(); ?> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $sticky=get_option('sticky_posts'); $args=array( 'cat'=>3, 'caller_get_posts'=>1, 'post__not_in' => $sticky, 'paged'=>$paged, 'posts_per_page' => 1 ); query_posts($args); ?> <!-- The following tests if the current post is in category 3. --> <!-- If it is, the div box is given the CSS class "post-cat-three". --> <!-- Otherwise, the div box will be given the CSS class "post". --> <?php if ( in_category('3') ) { ?> <div class="post-cat-three"> <?php } else { ?> <div class="post"> <?php } ?> <!-- Display the Title as a link to the Post's permalink. --> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/latestnews.png" style="margin-top:5px; margin-left:0;"/></a></h2> <small>Author: <?php the_author_posts_link(); ?></small> <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. --> <div class="datebg"> <span class="date"> <?php the_time('F j, Y') ?> </span> </div> <!-- Display the Post's Content in a div box. --> <div class="entry"> <?php $entry = the_content(); $limit = 200; substr($entry,0,$limit); ?> <table class="postnav" style="margin-top:5px;"> <tr> <?php if(get_next_posts_link()) { $next_posts_img = '<img src="' . get_bloginfo('template_url') . '/images/newer.png" class="newer" />'; ?> <td><?php next_posts_link($next_posts_img); ?></td> <?php } else { ?><td><img src="<?php bloginfo('template_url'); ?>/images/newernone.png" class="newer" /></td> <?php } ?> <td><a href="#"><img src="<?php bloginfo('template_url'); ?>/images/readmore.png" class="readmore" /></a></td> <?php if(get_previous_posts_link()) { $prev_posts_img = '<img src="' . get_bloginfo('template_url') . '/images/older.png" class="older" />'; ?> <td><?php previous_posts_link($prev_posts_img); ?></td> <?php } else { ?><td><img src="<?php bloginfo('template_url'); ?>/images/oldernone.png" class="older" /></td> <?php } ?> </tr> </table> </div> </div> <!-- closes the first div box --> <!-- Stop The Loop (but note the "else:" - see next line). --> <?php endwhile; ?> <?php else: ?> <!-- The very first "if" tested to see if there were any Posts to --> <!-- display. This "else" part tells what do if there weren't any. --> <p>Sorry, no posts matched your criteria.</p> <!-- REALLY stop The Loop. --> <?php endif; ?>
I need a fix soon
We need a link to the blog page on your site. And, you need to move the query_posts call to the top, like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $sticky=get_option('sticky_posts'); $args=array( 'cat'=>3, 'caller_get_posts'=>1, 'post__not_in' => $sticky, 'paged'=>$paged, 'posts_per_page' => 1 ); query_posts($args); ?> <?php if ( have_posts() ) : the_post(); ?> <?php while (have_posts()) : the_post(); ?>
I have it all working the way I want it to. Thanks for all the help
- The topic ‘Next and Previous Posts’ is closed to new replies.