Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

post_nav_link WordPress

  • SOLVED

Hey,

I got a pretty simple and basic loop and I'm looking to tweak the previous/next_post_link

Here's what I have now:


<div id="postnavi">
<div class="alignleft"><?php previous_posts_link(); ?></div>
<div class="alignright"><?php next_posts_link(); ?></div>
</div>


And I would like to display a list of the previous posts, the ones that show up on page/2/ and page/3/. Basically what I'd like is to offset one list by 10 (or any other number) and the other list by 20 so I have 2 lists on each subsequent page.

A good example of what I mean would be http://www.treehugger.com/

See at the bottom where it says 'Keep Rolling. Hit the Next Page >' that whole section is what I'm looking for. I was able to get it to work on the homepage by create a WP query and offsetting the list, but then on page/2 it stops working (obviously)

Any help with this would be greatly appreciated ;)

EDIT: I think Lew's solution would work pretty well. Lemme just try it out and I'll select the winner shortly. Thanks!

Answers (3)

2010-04-16

Lew Ayotte answers:

At the end of your loop, you should run another query_post

something like:

query_posts('posts_per_page=10&offset=10');

and

query_posts('posts_per_page=10&offset=20');

after each query_posts you'll have:

if (have_posts()) : while (have_posts()) : the_post() :
<a href="the_permalink()">the_title()</a>

excuse the poor PHP/HTML formatting :)

You'll need a way to keep track of the actual offset too... let me think about it for a minute.

Lew


Lew Ayotte comments:

Ok, so the offset variable will need to be calculated something like:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$offset1 = $paged * 10;
$offset2 = $offset1 + 10;

query_posts('posts_per_page=10&offset=' . $offset1 . '&paged='. $paged);

your loop...

then...

query_posts('posts_per_page=10&offset=' . $offset2 . '&paged='. $paged);

your loop...

does that make sense?

Lew


Lew Ayotte comments:

Actually, you know what...

you shouldn't need to calculate the offset if you're using paged...

so this would be it:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts('posts_per_page=10&offset=10&paged='. $paged);

your loop...

then...

query_posts('posts_per_page=10&offset=20&paged='. $paged);

your loop...


Lew Ayotte comments:

Here's an example of my code:

http://phrugal.com/

just the default theme, but this is the entire code block:

<blockquote><?php
/**
* @package WordPress
* @subpackage Default_Theme
*/

get_header(); ?>

<div id="content" class="narrowcolumn" role="main">

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
</div>

<?php endwhile; ?>

<div class="navigation">
<div class="alightright"><?php next_posts_link('Next 10 &raquo;') ?></div>
</div>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<?php query_posts('posts_per_page=10&offset=10&paged='. $paged); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>

<?php endwhile; endif; ?>


<div class="navigation">
<div class="alightright"><a href="<?php echo get_pagenum_link($paged + 2) ?>">Next 10 after that &raquo;</a></div>
</div>

<?php query_posts('posts_per_page=10&offset=20&paged='. $paged); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>

<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

<?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; ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?></blockquote>


Lew Ayotte comments:

whoops...

<?php
/**
* @package WordPress
* @subpackage Default_Theme
*/

get_header(); ?>

<div id="content" class="narrowcolumn" role="main">

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
</div>

<?php endwhile; ?>

<div class="navigation">
<div class="alightright"><?php next_posts_link('Next 10 &raquo;') ?></div>
</div>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<?php query_posts('posts_per_page=10&offset=10&paged='. $paged); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>

<?php endwhile; endif; ?>


<div class="navigation">
<div class="alightright"><a href="<?php echo get_pagenum_link($paged + 2) ?>">Next 10 after that &raquo;</a></div>
</div>

<?php query_posts('posts_per_page=10&offset=20&paged='. $paged); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>

<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

<?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; ?>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>


Lew Ayotte comments:

You'll notice... to get the page/3/ link I used the get_pagenum_link function w/ the $paged var:


<div class="navigation">
<div class="alightright"><a href="<?php echo get_pagenum_link($paged + 2) ?>">Next 10 after that &raquo;</a></div>
</div>


Lew Ayotte comments:

You'll also need a way to handle the "last page"... so it doesn't try to go to a "next" page...

Tree Hugger's second to last page - http://www.treehugger.com/page11.php
Tree Hugger's last page - http://www.treehugger.com/page12.php


Something like:

global $wp_query;

if ( !$max_page ) {
$max_page = $wp_query->max_num_pages;
}

if ($paged <= $max_page) {
// do stuff
}

2010-04-16

Erez S answers:

I don't realy understand what do you need,but in the end you said that you tried to create query but the pages don't worked,well the solution is simple:

query_posts("category_name=somecat&".((get_query_var('paged')) ? get_query_var('paged') : 1));

Enjoy


Erez S comments:

This is full code to your problem:
<?php
$pagenum = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=10&paged='. ($pagenum+1));
if(have_posts()): ?>
<div style="float:left;"><h2>The <?php next_posts_link('Next Page'); ?> Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
query_posts('posts_per_page=10&paged='. ($pagenum+2));
if(have_posts()): ?>
<div style="float:right;"><h2>The Page After That Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
?><br style="clear:both;" />

Enjoy


Erez S comments:

And here is improvment which will show random posts if it the last page or page before last:
<?php
$pagenum = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=10&paged='. ($pagenum+1));
if(have_posts()): ?>
<div style="float:left;"><h2>The <?php next_posts_link('Next Page'); ?> Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
else: $nopost=true;
query_posts('posts_per_page=10&orderby=rand');
if(have_posts()): ?>
<div style="float:left;"><h2>Articles Beyond Include:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
endif;
query_posts('posts_per_page=10&paged='. ($pagenum+2));
if(have_posts()): ?>
<div style="float:right;"><h2>The Page After That Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
else:
query_posts('posts_per_page=10&orderby=rand');
if(have_posts()): ?>
<div style="float:left;"><h2><?php if(isset($nopost)){ ?>And Also...<?php }else{ ?> Articles Beyond Include: <?php } ?></h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
endif;
?><br style="clear:both;" />


Erez S comments:

My bad:
<?php
$pagenum = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=10&paged='. ($pagenum+1));
if(have_posts()): ?>
<div style="float:left;"><h2>The <?php next_posts_link('Next Page'); ?> Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
else: $nopost=true;
query_posts('posts_per_page=10&orderby=rand');
if(have_posts()): ?>
<div style="float:left;"><h2>Articles Beyond Include:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
endif;
query_posts('posts_per_page=10&paged='. ($pagenum+2));
if(have_posts()): ?>
<div style="float:right;"><h2>The Page After That Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
else:
query_posts('posts_per_page=10&orderby=rand');
if(have_posts()): ?>
<div style="float:left;"><h2><?php if(isset($nopost)){ ?>And Also...<?php }else{ ?> Articles Beyond Include: <?php } ?></h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
endif;
?><br style="clear:both;" />


Erez S comments:

Sorry,forgot something realy small,you won't notice it,but here is the new code:
<?php
$pagenum = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=10&paged='. ($pagenum+1));
if(have_posts()): ?>
<div style="float:left;"><h2>The <?php next_posts_link('Next Page'); ?> Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
else: $nopost=true;
query_posts('posts_per_page=10&orderby=rand');
if(have_posts()): ?>
<div style="float:left;"><h2>Articles Beyond Include:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
endif;
query_posts('posts_per_page=10&paged='. ($pagenum+2));
if(have_posts()): ?>
<div style="float:right;"><h2>The <a href="<?php echo get_pagenum_link($pagenum+2) ?>">Page After</a> That Contains:</h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
else:
query_posts('posts_per_page=10&orderby=rand');
if(have_posts()): ?>
<div style="float:left;"><h2><?php if(isset($nopost)){ ?>And Also...<?php }else{ ?> Articles Beyond Include: <?php } ?></h2>
<?php while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?></div><?php
endif;
endif;
?><br style="clear:both;" />

2010-04-16

Utkarsh Kukreti answers:

Are you adding this to single post, or the page that lists many posts? (index, page/2, page/3)?


Utkarsh Kukreti comments:

Try this code
<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;


query_posts('posts_per_page=10&paged='. ($paged+1));
echo 'The next page contains';
if(have_posts()): while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile; endif;


query_posts('posts_per_page=10&paged='. ($paged+2));
echo 'The page after that contains';
if(have_posts()): while(have_posts()) : ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile; endif;
?>