我正在寻找一种简洁的方法get_next_post()
一旦它击中最后一个帖子,再回到开头。
目前,它一旦到达最终职位就会停止。
以下是代码中的几行代码
对于与我正在使用的相似的上下文:
<?php $next_post = get_next_post();
if (!empty( $next_post )): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo $next_post->post_title; ?>
</a>
<?php endif; ?>
http://codex.wordpress.org/Function_Reference/get_next_post
提前感谢您的建议。
答案 0 :(得分:0)
你不能用get_next_post - 完全停止。
这是我如何做到的......
<?php
/**
* Infinite next and previous post looping in WordPress
*/
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
$prev_title = strip_tags(str_replace('"', '', $previous_post->post_title));
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
// get the first posts ID etc
$args = array('post_type'=>'project', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID;
$first_title = get_the_title( $first_id );
// get the last posts ID etc
$last_post = end($posts);
$last_id = $last_post->ID; // To get ID of last post in custom post type outside of loop
$last_title = get_the_title( $last_id );
// if the current post isn't the first post
if($current_id != $first_id) { ?>
<a href="<?php echo get_permalink($previous_id) ?>" title="<?php $prev_title ?>" class="prev-next prev">Previous Project:<?php echo $prev_title; ?></a>
<?php
// if the current post is the first post
} else { ?>
<a href="<?php echo get_permalink($last_id) ?>" title="<?php $last_title ?>" class="prev-next prev"> Previous Project</a>
<?php }; ?>
<?php
// if the current post isn't the last post
if($current_id != $last_id) { ?>
<a rel="next" href="<?php echo get_permalink($next_id) ?>" title="<?php $next_title ?>" class="prev-next next"> Next Project <?php echo $next_title; ?></a>
<?php
// if the current post is the last post
} else { ?>
<a rel="next" href="<?php echo get_permalink($first_id) ?>" title="<?php $last_title ?>" class="prev-next next"> Next Project <?php echo $first_title; ?></a>
<?php } ?>
元素取自Getting first & last post in custom post type outside of loop