質問編集履歴
2
追記2
test
CHANGED
File without changes
|
test
CHANGED
@@ -52,10 +52,6 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
-
# 追記
|
56
|
-
|
57
|
-
|
58
|
-
|
59
55
|
```PHP
|
60
56
|
|
61
57
|
function whats_new()
|
@@ -96,7 +92,7 @@
|
|
96
92
|
|
97
93
|
```
|
98
94
|
|
99
|
-
|
95
|
+
#追記
|
100
96
|
|
101
97
|
CHERRYさんの回答でここまで進められました。
|
102
98
|
|
@@ -141,3 +137,167 @@
|
|
141
137
|
|
142
138
|
|
143
139
|
こういった形にするには、どうしたらよいでしょうか?
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
#追記2
|
144
|
+
|
145
|
+
hotaruikaさんのコードを使わせてもらいました。
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
- 投稿と、固定ページと、カスタム投稿タイプportfolio
|
150
|
+
|
151
|
+
- **foreach**のところ、元のコードだと、**Warning: Invalid argument supplied for foreach()**が出たので、**(array)**を追加しました。
|
152
|
+
|
153
|
+
- **最後に、**ショートコード にしました。
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
すると
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
2019.8.1
|
162
|
+
|
163
|
+
固定ページ
|
164
|
+
|
165
|
+
固定ページリンク
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
**foreach**に問題があるのか、記事が一つしか表示されなかったです、
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
実際のコードです。
|
174
|
+
|
175
|
+
```php
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
function my_get_posts()
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
$my_posts= get_posts(
|
184
|
+
|
185
|
+
array(
|
186
|
+
|
187
|
+
'post_type' => array('post','page','portfolio'), //post_typeを複数指定
|
188
|
+
|
189
|
+
'posts_per_page' => 10 //表示させる記事数
|
190
|
+
|
191
|
+
)
|
192
|
+
|
193
|
+
);
|
194
|
+
|
195
|
+
return $my_posts;
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
// 指定した $post_id のカテゴリ一覧を出力する関数
|
202
|
+
|
203
|
+
function my_echo_categories( $post_id )
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
$categories = get_the_category();
|
208
|
+
|
209
|
+
foreach ( $categories as $category )
|
210
|
+
|
211
|
+
{
|
212
|
+
|
213
|
+
$result="";
|
214
|
+
|
215
|
+
$result.='<ul>';
|
216
|
+
|
217
|
+
$result.='<li>' . $category->name . '</li>';
|
218
|
+
|
219
|
+
$result.='</ul>';
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
return $result;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
// 取得した投稿データ群($my_posts)を使って出力する関数
|
236
|
+
|
237
|
+
function whats_new( $my_posts )
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
// 投稿データ群を1件ずつ処理
|
242
|
+
|
243
|
+
foreach((array)$my_posts as $post):setup_postdata($post);
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// 投稿データを取得
|
248
|
+
|
249
|
+
$post_id = $post->ID;
|
250
|
+
|
251
|
+
$post_type_name = get_post_type_object(get_post_type())->name;
|
252
|
+
|
253
|
+
$post_type_label = get_post_type_object(get_post_type())->label;
|
254
|
+
|
255
|
+
$post_date = get_the_date("Y.n.j");
|
256
|
+
|
257
|
+
$post_href = get_permalink();
|
258
|
+
|
259
|
+
$post_title = get_the_title();
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
// 出力
|
264
|
+
|
265
|
+
$result ="";
|
266
|
+
|
267
|
+
$result.='<div>';
|
268
|
+
|
269
|
+
$result.='<div class="news_day">'. $post_date .'</div>';
|
270
|
+
|
271
|
+
$result.='<div class="'. $post_type_name .'">'. $post_type_label .'</div>';
|
272
|
+
|
273
|
+
if( $post_type_name=='post' ){ // 投稿の場合だけカテゴリを出力
|
274
|
+
|
275
|
+
my_echo_categories( $post_id );
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
$result.='<div class="news_txt"><a href="'. $post_href .'">'. $post_title .'</a></div>';
|
280
|
+
|
281
|
+
$result.='</div>';
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
return $result;
|
288
|
+
|
289
|
+
endforeach ;
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
wp_reset_postdata();
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
add_shortcode('whats_new2','whats_new');
|
302
|
+
|
303
|
+
```
|
1
現在の状況と、具体例を追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -49,3 +49,95 @@
|
|
49
49
|
|
50
50
|
|
51
51
|
またこういったときの、コピペの方法について、参考ページがあったら教えてください。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
# 追記
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
```PHP
|
60
|
+
|
61
|
+
function whats_new()
|
62
|
+
|
63
|
+
{
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
global $post;
|
68
|
+
|
69
|
+
$my_posts= get_posts(array(
|
70
|
+
|
71
|
+
'post_type' => array('post','page'), //post_typeを複数指定
|
72
|
+
|
73
|
+
'numberposts' => 10 //表示させる記事数
|
74
|
+
|
75
|
+
));
|
76
|
+
|
77
|
+
foreach($my_posts as $post):setup_postdata($post);
|
78
|
+
|
79
|
+
echo '
|
80
|
+
|
81
|
+
<div class="news_day">'. get_the_date("Y.n.j") .'</div>
|
82
|
+
|
83
|
+
<div class="'. esc_html(get_post_type_object(get_post_type())->name) .'">'. esc_html(get_post_type_object(get_post_type())->label)
|
84
|
+
|
85
|
+
,esc_html(the_category(' ',' ','')) .' </div>
|
86
|
+
|
87
|
+
<div class="news_txt"><a href="'. get_permalink() .'">'. get_the_title() .'</a></div>' ;
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
endforeach ;
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
CHERRYさんの回答でここまで進められました。
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
2019.8.1
|
108
|
+
|
109
|
+
投稿
|
110
|
+
|
111
|
+
投稿リンク
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
2019.8.1
|
116
|
+
|
117
|
+
固定ページ
|
118
|
+
|
119
|
+
固定ページリンク
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
と出力されました。
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
あともう一歩、
|
128
|
+
|
129
|
+
下記の様に、投稿の場合のときのみ、仕切りを入れて、かつカテゴリ名を表示させたいです。
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
2019.8.1
|
134
|
+
|
135
|
+
投稿 / カテゴリー名
|
136
|
+
|
137
|
+
リンク付き投稿タイトル
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
こういった形にするには、どうしたらよいでしょうか?
|