Files
wordpress-theme-aksrt/templates/parts/docs/doc-chapter-nav.php
T

71 lines
2.4 KiB
PHP

<?php
/**
* Document Chapter Navigation
*
* Shows previous/next chapter navigation at the bottom of a document page.
* Uses AKSRT_Docs_Presenter for chapter resolution.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
$current_id = get_the_ID();
$presenter = class_exists('AKSRT_Docs_Presenter') ? AKSRT_Docs_Presenter::get_instance() : null;
if ($presenter) {
$nav = $presenter->get_chapter_nav($current_id);
$prev_page = $nav['prev'];
$next_page = $nav['next'];
} else {
$parent_id = wp_get_post_parent_id($current_id);
$siblings = get_pages([
'parent' => $parent_id ?: 0,
'sort_column' => 'menu_order, post_title',
'sort_order' => 'ASC',
'post_status' => 'publish',
]);
$current_index = -1;
foreach ($siblings as $i => $sibling) {
if ((int) $sibling->ID === $current_id) {
$current_index = $i;
break;
}
}
$prev_page = $current_index > 0 ? $siblings[$current_index - 1] : null;
$next_page = ($current_index >= 0 && $current_index < count($siblings) - 1)
? $siblings[$current_index + 1]
: null;
}
if (!$prev_page && !$next_page) return;
?>
<nav class="aksrt-doc-chapter-nav" aria-label="<?php esc_attr_e('章节导航', 'aksrt'); ?>">
<?php if ($prev_page) : ?>
<a href="<?php echo esc_url(get_permalink($prev_page->ID)); ?>" class="aksrt-doc-chapter-nav__prev">
<span class="aksrt-doc-chapter-nav__label">
<i class="fas fa-arrow-left"></i>
<?php esc_html_e('上一篇', 'aksrt'); ?>
</span>
<span class="aksrt-doc-chapter-nav__title"><?php echo esc_html($prev_page->post_title); ?></span>
</a>
<?php else : ?>
<div class="aksrt-doc-chapter-nav__prev aksrt-doc-chapter-nav__prev--disabled"></div>
<?php endif; ?>
<?php if ($next_page) : ?>
<a href="<?php echo esc_url(get_permalink($next_page->ID)); ?>" class="aksrt-doc-chapter-nav__next">
<span class="aksrt-doc-chapter-nav__label">
<?php esc_html_e('下一篇', 'aksrt'); ?>
<i class="fas fa-arrow-right"></i>
</span>
<span class="aksrt-doc-chapter-nav__title"><?php echo esc_html($next_page->post_title); ?></span>
</a>
<?php else : ?>
<div class="aksrt-doc-chapter-nav__next aksrt-doc-chapter-nav__next--disabled"></div>
<?php endif; ?>
</nav>