1
votes

I am using custom page templates to structure different blog layouts in my WordPress theme that I want to sell. Everything is functioning fine except the post_nav_link navigation (previous post | next post). The wordpress codex says that post_nav_links won't work with custom page templates, but I really don't want to start all over again. Is there anything I can do to make post_nav_link navigation work with custom page templates?

Codex Refernece: http://codex.wordpress.org/Next_and_Previous_Links

2
Are they pages or posts? Have you tried the suggestion at the bottom of the codex? - Christian
IF I'm correct next and previous don't work on pages but only on posts. Based on what do you want to navigate? - janw
@ChristianVarga yes I have tried the solution at the bottom of the page, but it then turns my blog post navigation into a page-to-page navigation. For example, I'll click next page and it will take me to my contact page instead of the next page of blog posts. - James Banks
@janw I have set my custom blog page templates to display only 3 posts per page. I want to implement pagination so that the user can navigate to the pages that contain older and newer posts. - James Banks
Ah now I get it. I'll get back at you later today. - janw

2 Answers

1
votes

Try this, it works for my custom template, you might need to add args to query_posts but the key is an offset.

$paged = get_query_var('paged');

$offset = 0;
if ($paged != 0 ) {
    //$paged -1 because there is no page 1, just 0 and 2 And page 0 is skipped
    $offset = ($paged-1) * get_query_var('posts_per_page') ;
}
query_posts('offset=' . $offset);
if (have_posts()) : while (have_posts()) : the_post();
       // the loop

and for the pagination:

<div id="pagination">
    <div id="pagination-previous"><?php previous_posts_link('previous'); ?></div>
    <div id="pagination-next"><?php next_posts_link('next'); ?></div>
</div>
0
votes

Thanks @janw, I will try this in the morning. Before I do so, can you cofirm with me if this is the right way to PHP tag the first lot of code?

        <?php query_posts("posts_per_page=3"); ?> <!-- Do I keep this line? -->
        <?php $paged = get_query_var('paged'); ?>
        <?php $offset = 0;
        if ($paged != 0 ) {
            //$paged -1 because there is no page 1, just 0 and 2 And page 0 is skipped
            $offset = ($paged-1) * get_query_var('posts_per_page') ;
        } ?>
        <?php query_posts('offset=' . $offset); ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>