WordPressメモ 前後のページへのリンク

single.php

<?php $next_post = get_next_post(); ?>
<?php $previous_post = get_previous_post(); ?>
<?php if(!(empty($next_post) && empty($previous_post))): ?>
<div class="navi">
<ul>
<?php if(!empty($previous_post)): ?>
<li><?php previous_post_link( '%link', '<span>&laquo;</span> %title' ) ?></li>
<?php endif ; ?>
<?php if(!empty($next_post)): ?>
<li><?php next_post_link( '%link', '%title <span>&raquo;</span>' ) ?></li>
<?php endif ; ?>
</ul>
</div>

index.php

<?php $next_posts_link = get_next_posts_link('« 前のページ',0); ?>
<?php $previous_posts_link = get_previous_posts_link('次のページ »',0); ?>
<?php if((isset($next_posts_link)) || (isset($previous_posts_link))): ?>
<div>
<ul>
<?php if(isset($next_posts_link)): ?>
<li><?php echo $next_posts_link; ?></li>
<?php endif ; ?>
<?php if(isset($previous_posts_link)): ?>
<li><?php echo $previous_posts_link; ?></li>
<?php endif ; ?>
</ul>
</div>
<?php endif ; ?>