ホーム » N-PLUS 全記事一覧 » Wordpress » 【WordPress】各記事に「前の記事」・「次の記事」の設置
【WordPress】各記事に「前の記事」・「次の記事」の設置
WordPress
■矢印&記事タイトル(デフォルト)
// 前の記事へのリンク
<?php previous_post_link(); ?>
// 次の記事へのリンク
<?php next_post_link(); ?>
■画像付きの「前の記事」・「次の記事」の設置
<?php
$prevpost = get_adjacent_post(true, '', true); //前の記事
$nextpost = get_adjacent_post(true, '', false); //次の記事
if( $prevpost or $nextpost ){ //前の記事、次の記事いずれか存在しているとき
?>
<ul>
<?php
if ( $prevpost ) { //前の記事が存在しているとき
echo '<li><div>' . get_the_post_thumbnail($prevpost->ID, 'thumbnail') . '</div><a href="' . get_permalink($prevpost->ID) . '">' . get_the_title($prevpost->ID) . '</a></li>';
} else { //前の記事が存在しないとき
echo '<li><div><a href="' . network_site_url('/') . '">TOPへ戻る</a></div></li>';
}
if ( $nextpost ) { //次の記事が存在しているとき
echo '<li><div>' . get_the_post_thumbnail($nextpost->ID, 'thumbnail') . '</div><a href="' . get_permalink($nextpost->ID) . '">' . get_the_title($nextpost->ID) . '</a></li>';
} else { //次の記事が存在しないとき
echo '<li><div><a href="' . network_site_url('/') . '">TOPへ戻る</a></div></li>';
}
?>
</ul>
<?php } ?>