同じ親を持つ固定ページで前後リンクをつける
投稿ページでは前後の記事へのリンクを表示する為のテンプレートタグがありますが、固定ページでは機能しません。
同じ親ページを持つ固定ページで前後リンクをつけたいと思い調べました。
<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
$ancestors = get_post_ancestors( $post );
$ancestor = ( $ancestors ) ? $ancestors[count( $ancestors ) - 1] : $post->ID;
$prev_ancestors = get_post_ancestors( $prevID );
$prev_ancestor = ( $prev_ancestors ) ? $prev_ancestors[count( $prev_ancestors ) - 1] : $prevID;
$next_ancestors = get_post_ancestors( $nextID );
$next_ancestor = ( $next_ancestors ) ? $next_ancestors[count( $next_ancestors ) - 1] : $nextID;
?>
<div class="navigation">
<?php if ($prev_ancestor == $ancestor):?>
<a href="<?php echo get_permalink($prevID); ?>"
title="<?php echo get_the_title($prevID); ?>"><?php echo get_the_title($prevID); ?></a> <<
<?php endif;?>
<?php if ($next_ancestor == $ancestor):?>
>> <a href="<?php echo get_permalink($nextID); ?>"
title="<?php echo get_the_title($nextID); ?>"><?php echo get_the_title($nextID); ?></a>
<?php endif;?>
</div><!-- .navigation -->
RECENT POSTS
SEARCH





