Skip to content

Commit 9b5a42a

Browse files
tree walk to find prev/next pagination links if present based on siblings, or cousins
1 parent 2af3dd9 commit 9b5a42a

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

src/single-course.php

+79-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646

4747
<?php while ( have_posts() ) : the_post(); ?>
4848

49+
<?php
50+
$parent = get_post($post->post_parent);
51+
$grandparent = get_post($parent->post_parent);
52+
$aunts = get_children($grandparent->ID);
53+
?>
54+
4955
<header>
5056

5157
<nav class="breadcrumbs">
@@ -61,7 +67,9 @@
6167
<?php endif; ?>
6268

6369
<?php if ($contextType != 'course-index' ) : ?>
64-
<li><a href="#">top title of course</a></li>
70+
<li><a href="<?php the_permalink($grandparent->ID); ?>"><?php echo $grandparent->post_title; ?></a></li>
71+
72+
6573
<?php endif; ?>
6674

6775
</ul>
@@ -76,14 +84,84 @@
7684

7785
</header>
7886

87+
7988
<div class="content">
8089

8190

8291
<?php the_content(); ?>
8392

93+
<?php
94+
95+
// find previous & next siblings, if present
96+
if (has_post_parent()) {
97+
$siblings = get_children($post->post_parent);
98+
99+
foreach ($siblings as $sibling ){
100+
if ($sibling->menu_order == $post->menu_order - 1) {
101+
$previousLink = get_permalink($sibling->ID);
102+
}
103+
104+
if ($sibling->menu_order == $post->menu_order + 1) {
105+
$nextLink = get_permalink($sibling->ID);
106+
}
107+
}
108+
}
109+
110+
if ($previousLink == '') {
111+
112+
foreach ($aunts as $aunt) {
113+
114+
if ($aunt->menu_order == $parent->menu_order - 1) {
115+
116+
$cousins = get_children($aunt->ID);
117+
118+
foreach ($cousins as $cousin) {
119+
if ($cousin->menu_order == count($cousins)) {
120+
$previousLink = get_permalink($cousin->ID);
121+
}
122+
123+
}
124+
}
125+
}
126+
}
127+
128+
if ($nextLink == '' && $contextType == 'course-page') {
129+
130+
foreach ($aunts as $aunt) {
131+
132+
if ($aunt->menu_order == $parent->menu_order + 1) {
133+
134+
$cousins = get_children($aunt->ID);
135+
136+
foreach ($cousins as $cousin) {
137+
if ($cousin->menu_order == 1) {
138+
$nextLink = get_permalink($cousin->ID);
139+
}
140+
141+
}
142+
}
143+
}
144+
}
145+
146+
?>
84147

85148
</div>
86149

150+
<?php if ($previousLink != '' || $nextLink != '') : ?>
151+
<nav class="pagination" aria-label="Pagination">
152+
<ul>
153+
<?php if ($previousLink != '') : ?>
154+
<li><a href="<?php echo $previousLink; ?>">Previous</a></li>
155+
<?php endif; ?>
156+
157+
<?php if ($nextLink != '') : ?>
158+
<li><a href="<?php echo $nextLink; ?>">Next</a></li>
159+
<?php endif; ?>
160+
</ul>
161+
</nav>
162+
<?php endif; ?>
163+
164+
87165

88166
<?php endwhile; // end of the loop. ?>
89167
</main>

0 commit comments

Comments
 (0)