質問編集履歴

6

質問内容の不足分を追加

2018/09/05 03:08

投稿

mochiko
mochiko

スコア12

test CHANGED
File without changes
test CHANGED
@@ -82,6 +82,8 @@
82
82
 
83
83
 
84
84
 
85
+ 現在見ているページの子カテゴリーの取得が上手くできておりません。
86
+
85
87
 
86
88
 
87
89
  何か適切な記述方法がありましたらアドバイスをいただきたいです。

5

コード修正

2018/09/05 03:08

投稿

mochiko
mochiko

スコア12

test CHANGED
File without changes
test CHANGED
@@ -32,13 +32,15 @@
32
32
 
33
33
  $post_id = get_the_ID();
34
34
 
35
- $categories = get_the_category();
35
+ $cat = get_category_by_slug($post_id);
36
36
 
37
- $child_cat = $categories->slug;
37
+ $cat_id = intval($cat->cat_ID);
38
+
39
+ $childs = get_term_children($cat_id, 'category');
38
40
 
39
41
 
40
42
 
41
- foreach( $child_cat as $category ):
43
+ foreach( $childs as $category ):
42
44
 
43
45
  $args = array(
44
46
 

4

コード修正

2018/09/05 03:06

投稿

mochiko
mochiko

スコア12

test CHANGED
File without changes
test CHANGED
@@ -30,47 +30,51 @@
30
30
 
31
31
  <?php
32
32
 
33
- global $post;
33
+ $post_id = get_the_ID();
34
34
 
35
- $currentID = get_the_ID();
35
+ $categories = get_the_category();
36
36
 
37
- $cat = get_the_category();
38
-
39
- $child_cat = $cat->slug;
37
+ $child_cat = $categories->slug;
40
38
 
41
39
 
42
40
 
41
+ foreach( $child_cat as $category ):
42
+
43
43
  $args = array(
44
-
45
- 'post_type' => 'post',
46
44
 
47
45
  'posts_per_page' => 4,
48
46
 
49
- 'category_name' => $child_cat,
47
+ 'category' => $category->cat_ID,
50
48
 
51
- 'post__not_in' => array($currentID)
49
+ 'exclude' => $post_id
52
50
 
53
51
  );
54
52
 
53
+ $posts = get_posts( $args );
54
+
55
+
56
+
57
+ if ( sizeof($posts) > 0 ):
58
+
59
+ echo '<h3>'.$category->cat_name.'と同カテゴリの記事一覧</h3>';
60
+
61
+ echo '<ul>';
62
+
63
+ foreach( $posts as $post ):
64
+
65
+ echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
66
+
67
+ endforeach;
68
+
69
+ echo '</ul>';
70
+
71
+ endif;
72
+
73
+ endforeach;
74
+
75
+ $post = get_post($post_id);
76
+
55
77
  ?>
56
-
57
- <?php $myPosts = get_posts($args); if($myPosts) : ?>
58
-
59
- <ul>
60
-
61
- <?php foreach($myPosts as $post) : setup_postdata($post); ?>
62
-
63
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
64
-
65
- <?php endforeach; ?>
66
-
67
- </ul>
68
-
69
- <?php else : ?>
70
-
71
- <p>関連する記事はまだありません。</p>
72
-
73
- <?php endif; wp_reset_postdata(); ?>
74
78
 
75
79
  ```
76
80
 

3

現状のコードの追記

2018/09/05 03:01

投稿

mochiko
mochiko

スコア12

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,58 @@
24
24
 
25
25
 
26
26
 
27
+ 現状、single.phpに以下のような記述をしております(大分的外れかもしれません)
28
+
29
+ ```wordpress
30
+
31
+ <?php
32
+
33
+ global $post;
34
+
35
+ $currentID = get_the_ID();
36
+
37
+ $cat = get_the_category();
38
+
39
+ $child_cat = $cat->slug;
40
+
41
+
42
+
43
+ $args = array(
44
+
45
+ 'post_type' => 'post',
46
+
47
+ 'posts_per_page' => 4,
48
+
49
+ 'category_name' => $child_cat,
50
+
51
+ 'post__not_in' => array($currentID)
52
+
53
+ );
54
+
55
+ ?>
56
+
57
+ <?php $myPosts = get_posts($args); if($myPosts) : ?>
58
+
59
+ <ul>
60
+
61
+ <?php foreach($myPosts as $post) : setup_postdata($post); ?>
62
+
63
+ <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
64
+
65
+ <?php endforeach; ?>
66
+
67
+ </ul>
68
+
69
+ <?php else : ?>
70
+
71
+ <p>関連する記事はまだありません。</p>
72
+
73
+ <?php endif; wp_reset_postdata(); ?>
74
+
75
+ ```
76
+
77
+
78
+
27
79
 
28
80
 
29
81
  何か適切な記述方法がありましたらアドバイスをいただきたいです。

2

タイトルが分かりづらかったため訂正

2018/09/05 02:50

投稿

mochiko
mochiko

スコア12

test CHANGED
@@ -1 +1 @@
1
- 現在閲覧している記事の同一子カテゴリの記事一覧を表示させたい
1
+ 【WordPress】現在閲覧している記事の同一子カテゴリの記事一覧を表示させたい
test CHANGED
File without changes

1

文章の修正

2018/09/05 02:09

投稿

mochiko
mochiko

スコア12

test CHANGED
File without changes
test CHANGED
File without changes