Can post_nav_link navigation work with WordPress custom page templates?

一笑奈何 提交于 2019-12-30 05:35:07

问题


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


来源:https://stackoverflow.com/questions/10121230/can-post-nav-link-navigation-work-with-wordpress-custom-page-templates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!