teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コードを修正

2017/12/11 01:20

投稿

退会済みユーザー
answer CHANGED
@@ -5,20 +5,22 @@
5
5
  ```PHP
6
6
  <div>
7
7
  <dl>
8
- <?php
8
+ <?php
9
- $categories = get_the_category();
9
+ $categories = get_the_category();
10
- foreach ($categories as $category) {
10
+ foreach ($categories as $category) {
11
- if ($category->category_parent === 0) {
11
+ if ($category->category_parent === 0) {
12
- $parent_term_id = $category->term_id;
12
+ $parent_term_id = $category->term_id;
13
- } else {
13
+ } else {
14
- $child_term_id[] = $category->term_id;
14
+ $child_term_id[] = $category->term_id;
15
- }
15
+ }
16
+ }
17
+ if (is_array($child_term_id)) {
18
+ $term_children = get_term_children($parent_term_id, 'category');
19
+ $term_children = array_diff($term_children, $child_term_id);
20
+ $term_children = implode(',', $term_children);
16
21
  }
17
- $term_children = get_term_children($parent_term_id, 'category');
18
- $term_children = array_diff($term_children, $child_term_id);
19
- $term_children = implode(',', $term_children);
20
- $prev_post = get_previous_post(true, $term_children);
22
+ $prev_post = get_previous_post(true, $term_children);
21
- $next_post = get_next_post(true, $term_children);
23
+ $next_post = get_next_post(true, $term_children);
22
24
  if ( !empty( $prev_post ) ): ?>
23
25
  <dt>PREV</dt>
24
26
  <dd>
@@ -26,7 +28,7 @@
26
28
  </dd>
27
29
  <?php endif; ?>
28
30
  <?php
29
- $next_post = get_next_post(true, $term_children);
31
+ $next_post = get_next_post(true, $term_children);
30
32
  if ( !empty( $next_post ) ): ?>
31
33
  <dt>NEXT</dt>
32
34
  <dd>
@@ -34,7 +36,7 @@
34
36
  </dd>
35
37
  <?php endif; ?>
36
38
  </dl>
37
- </div>
39
+ </div>
38
40
  ```
39
41
  `get_previous_post()`が除外するIDを指定できるものの表示したいIDは指定できなかったので、少し回りくどくなってしまいましたが、
40
42
  これで変数`$prev_post`と`$next_post`にはサブカテゴリが一致する記事が入っているはずですので確認してみてください。

1

追記

2017/12/11 01:20

投稿

退会済みユーザー
answer CHANGED
@@ -3,21 +3,38 @@
3
3
  とりあえず該当の記事に設定されている子カテゴリのいずれかで一番目にヒットした記事を表示する仕様で書きます。
4
4
 
5
5
  ```PHP
6
+ <div>
7
+ <dl>
6
- <?php
8
+ <?php
7
- $categories = get_the_category();
9
+ $categories = get_the_category();
8
- foreach ($categories as $category) {
10
+ foreach ($categories as $category) {
9
- if ($category->category_parent === 0) {
11
+ if ($category->category_parent === 0) {
10
- $parent_term_id = $category->term_id;
12
+ $parent_term_id = $category->term_id;
11
- } else {
13
+ } else {
12
- $child_term_id[] = $category->term_id;
14
+ $child_term_id[] = $category->term_id;
15
+ }
13
16
  }
14
- }
15
- $term_children = get_term_children($parent_term_id, 'category');
17
+ $term_children = get_term_children($parent_term_id, 'category');
16
- $term_children = array_diff($term_children, $child_term_id);
18
+ $term_children = array_diff($term_children, $child_term_id);
17
- $term_children = implode(',', $term_children);
19
+ $term_children = implode(',', $term_children);
18
- $prev_post = get_previous_post(true, $term_children);
20
+ $prev_post = get_previous_post(true, $term_children);
19
- $next_post = get_next_post(true, $term_children);
21
+ $next_post = get_next_post(true, $term_children);
22
+ if ( !empty( $prev_post ) ): ?>
23
+ <dt>PREV</dt>
20
- ?>
24
+ <dd>
25
+ <a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>"><?php echo $prev_post->post_title; ?></a>
26
+ </dd>
27
+ <?php endif; ?>
28
+ <?php
29
+ $next_post = get_next_post(true, $term_children);
30
+ if ( !empty( $next_post ) ): ?>
31
+ <dt>NEXT</dt>
32
+ <dd>
33
+ <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>"><?php echo $next_post->post_title; ?></a>
34
+ </dd>
35
+ <?php endif; ?>
36
+ </dl>
37
+ </div>
21
38
  ```
22
39
  `get_previous_post()`が除外するIDを指定できるものの表示したいIDは指定できなかったので、少し回りくどくなってしまいましたが、
23
40
  これで変数`$prev_post`と`$next_post`にはサブカテゴリが一致する記事が入っているはずですので確認してみてください。