回答編集履歴

2

コードを修正

2017/12/11 01:20

投稿

退会済みユーザー
test CHANGED
@@ -12,33 +12,37 @@
12
12
 
13
13
  <dl>
14
14
 
15
- <?php
15
+ <?php
16
16
 
17
- $categories = get_the_category();
17
+ $categories = get_the_category();
18
18
 
19
- foreach ($categories as $category) {
19
+ foreach ($categories as $category) {
20
20
 
21
- if ($category->category_parent === 0) {
21
+ if ($category->category_parent === 0) {
22
22
 
23
- $parent_term_id = $category->term_id;
23
+ $parent_term_id = $category->term_id;
24
24
 
25
- } else {
25
+ } else {
26
26
 
27
- $child_term_id[] = $category->term_id;
27
+ $child_term_id[] = $category->term_id;
28
28
 
29
- }
29
+ }
30
+
31
+ }
32
+
33
+ if (is_array($child_term_id)) {
34
+
35
+ $term_children = get_term_children($parent_term_id, 'category');
36
+
37
+ $term_children = array_diff($term_children, $child_term_id);
38
+
39
+ $term_children = implode(',', $term_children);
30
40
 
31
41
  }
32
42
 
33
- $term_children = get_term_children($parent_term_id, 'category');
43
+ $prev_post = get_previous_post(true, $term_children);
34
44
 
35
- $term_children = array_diff($term_children, $child_term_id);
36
-
37
- $term_children = implode(',', $term_children);
38
-
39
- $prev_post = get_previous_post(true, $term_children);
40
-
41
- $next_post = get_next_post(true, $term_children);
45
+ $next_post = get_next_post(true, $term_children);
42
46
 
43
47
  if ( !empty( $prev_post ) ): ?>
44
48
 
@@ -54,7 +58,7 @@
54
58
 
55
59
  <?php
56
60
 
57
- $next_post = get_next_post(true, $term_children);
61
+ $next_post = get_next_post(true, $term_children);
58
62
 
59
63
  if ( !empty( $next_post ) ): ?>
60
64
 
@@ -70,7 +74,7 @@
70
74
 
71
75
  </dl>
72
76
 
73
- </div>
77
+ </div>
74
78
 
75
79
  ```
76
80
 

1

追記

2017/12/11 01:20

投稿

退会済みユーザー
test CHANGED
@@ -8,35 +8,69 @@
8
8
 
9
9
  ```PHP
10
10
 
11
- <?php
11
+ <div>
12
12
 
13
- $categories = get_the_category();
13
+ <dl>
14
14
 
15
- foreach ($categories as $category) {
15
+ <?php
16
16
 
17
- if ($category->category_parent === 0) {
17
+ $categories = get_the_category();
18
18
 
19
- $parent_term_id = $category->term_id;
19
+ foreach ($categories as $category) {
20
20
 
21
- } else {
21
+ if ($category->category_parent === 0) {
22
22
 
23
+ $parent_term_id = $category->term_id;
24
+
25
+ } else {
26
+
23
- $child_term_id[] = $category->term_id;
27
+ $child_term_id[] = $category->term_id;
28
+
29
+ }
24
30
 
25
31
  }
26
32
 
27
- }
33
+ $term_children = get_term_children($parent_term_id, 'category');
28
34
 
29
- $term_children = get_term_children($parent_term_id, 'category');
35
+ $term_children = array_diff($term_children, $child_term_id);
30
36
 
31
- $term_children = array_diff($term_children, $child_term_id);
37
+ $term_children = implode(',', $term_children);
32
38
 
33
- $term_children = implode(',', $term_children);
39
+ $prev_post = get_previous_post(true, $term_children);
34
40
 
35
- $prev_post = get_previous_post(true, $term_children);
41
+ $next_post = get_next_post(true, $term_children);
36
42
 
37
- $next_post = get_next_post(true, $term_children);
43
+ if ( !empty( $prev_post ) ): ?>
38
44
 
45
+ <dt>PREV</dt>
46
+
39
- ?>
47
+ <dd>
48
+
49
+ <a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>"><?php echo $prev_post->post_title; ?></a>
50
+
51
+ </dd>
52
+
53
+ <?php endif; ?>
54
+
55
+ <?php
56
+
57
+ $next_post = get_next_post(true, $term_children);
58
+
59
+ if ( !empty( $next_post ) ): ?>
60
+
61
+ <dt>NEXT</dt>
62
+
63
+ <dd>
64
+
65
+ <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>"><?php echo $next_post->post_title; ?></a>
66
+
67
+ </dd>
68
+
69
+ <?php endif; ?>
70
+
71
+ </dl>
72
+
73
+ </div>
40
74
 
41
75
  ```
42
76