Next and Previous loop

时间:2016-12-29 作者:DroDro

我正在制作一个wordpress网站,但我不需要什么帮助,

我拥有的是一个带有“下一步”和“上一步”按钮的功能,用于列出项目,但当我转到上一个项目时,“下一步”按钮不见了。

我想做的是,当我进入最后一个项目时,我点击“下一步”,然后引导我进入第一个项目,比如循环幻灯片

这是我的代码:

<?php if (has_term( \'type-1\', \'projecttype\' ) ) { ?>
    <div class="holder">
    <?php if ( have_posts() ) : ?>
    <div class="container-fluid">
        <div class="project-paginat">
            <?php if(get_post_meta($post->ID, "_type", true)){ ?>
            <ul class="list-inline">
                <li><a href="<?php echo get_home_url(); ?>/our-work">Back to Our Work</a></li>
                <li>>> &nbsp;<?php echo get_post_meta($post->ID, "_type", true); ?></li>
                <li><?php previous_post_link(\'%link\', \'&lt; Previous Project\', true, \'\', \'projectcategory\') ?></li> 
                <li><?php next_post_link(\'%link\', \'Next Project &gt;\', true, \'\', \'projectcategory\') ?></li>     
            </ul> 
            <?php } else {
                echo \'<p>There Is No Project Type To Display.</p>\';
            } ?>
        </div>

3 个回复
最合适的回答,由SO网友:iguanarama 整理而成

使用get_adjacent_post 首先看看是否有下一篇帖子:

if (get_adjacent_post (false, \'\', false)) {
    next_post_link (\'%link\', \'Next Project &gt;\', true, \'\', \'projectcategory\');
} else {
    // manually create link to first post here
}
This site 显示了实际获取第一篇文章链接的一种方法,例如使用WP\\u查询调用,然后调用get\\u permalink。确切的代码取决于您显示的帖子类型。

SO网友:Tunji

使用中的链接iguanarama\'s 回答,您的更新代码是:

<?php if (has_term( \'type-1\', \'projecttype\' ) ) { ?>
<div class="holder">
    <?php if ( have_posts() ) : ?>
    <div class="container-fluid">
        <div class="project-paginat">
            <?php if(get_post_meta($post->ID, "_type", true)){ ?>
                <ul class="list-inline">
                    <li><a href="<?php echo get_home_url(); ?>/our-work">Back to Our Work</a></li>
                    <li>>> &nbsp;<?php echo get_post_meta($post->ID, "_type", true); ?></li>
                    <li>
                    <?php
                        if( get_adjacent_post(false, \'\', true) ) {
                            previous_post_link(\'%link\', \'&lt; Previous Project\', true, \'\', \'projectcategory\');
                        } else {
                            $args = array(
                                \'posts_per_page\' => 1,
                                \'order\'          => \'DESC\',
                                \'category_name\'  => \'projectcategory\',
                            );
                            $first = new WP_Query( $args ); $first->the_post();
                            echo \'<a href="\' . get_permalink() . \'">&lt; Previous Project</a>\';
                            wp_reset_query();
                        };

                    ?>
                    </li>
                    <li>
                    <?php
                        if( get_adjacent_post(false, \'\', false) ) {
                            next_post_link(\'%link\', \'Next Project &gt;\', true, \'\', \'projectcategory\');
                        } else {
                            $args = array(
                                \'posts_per_page\' => 1,
                                \'order\'          => \'ASC\',
                                \'category_name\'  => \'projectcategory\',
                            );
                            $last = new WP_Query( $args ); $last->the_post();
                            echo \'<a href="\' . get_permalink() . \'">Next Project &gt;</a>\';
                            wp_reset_query();
                        };

                    ?>
                    </li>
                </ul>
            <?php } else {
                echo \'<p>There Is No Project Type To Display.</p>\';
            } ?>
        </div>
Reference: http://wplancer.com/infinite-next-and-previous-post-looping-in-wordpress/

SO网友:Nuno Sarmento

您可以使用get\\u nextigation\\u post,请参见下面的工作示例。

     <?php
        /**
         *  Infinite next and previous post looping in WordPress
         */
        if( get_adjacent_post(false, \'\', true) ) {
            previous_post_link(\'%link\', \'&larr; Previous Post\');
        } else {
            $first = new WP_Query(\'posts_per_page=1&order=DESC\'); $first->the_post();
                echo \'<a href="\' . get_permalink() . \'">&larr; Previous Post</a>\';
            wp_reset_query();
        };

        if( get_adjacent_post(false, \'\', false) ) {
            next_post_link(\'%link\', \'Next Post &rarr;\');
        } else {
            $last = new WP_Query(\'posts_per_page=1&order=ASC\'); $last->the_post();
                echo \'<a href="\' . get_permalink() . \'">Next Post &rarr;</a>\';
            wp_reset_query();
        };

    ?>

相关推荐

Ordering terms whilst in loop

我有一个页面模板,显示所有;“发布”;在两个自定义分类中,帖子显示在一个表中$type = $_GET[\'type\']; $category = $_GET[\'category\']; args = array( \'post-type\' => \'literature\', \'posts_per_page\' => -1, \'tax_query\' => array(