Can post_nav_link navigation work with WordPress c

2020-03-01 00:18发布

问题:

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

回答1:

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>


回答2:

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