質問編集履歴

6

解決しました。

2017/06/01 17:24

投稿

thewild
thewild

スコア32

test CHANGED
File without changes
test CHANGED
@@ -263,3 +263,197 @@
263
263
 
264
264
 
265
265
  よろしくお願いいたします。
266
+
267
+
268
+
269
+ ###解決しました。
270
+
271
+
272
+
273
+ 以下最終コード
274
+
275
+ ```php
276
+
277
+ <ul>
278
+
279
+ <?php
280
+
281
+ $args = array(
282
+
283
+ 'posts_per_page' => 7,
284
+
285
+ 'paged' => $paged,
286
+
287
+ 'orderby' => 'post_date',
288
+
289
+ 'post_type' => 'shop',
290
+
291
+ 'post_status' => 'publish'
292
+
293
+ );
294
+
295
+ $the_query = new WP_Query($args);
296
+
297
+ ?>
298
+
299
+ <?PHP if ( $the_query->have_posts() ) :?>
300
+
301
+ <?php $bloglist = array(); ?>
302
+
303
+ <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
304
+
305
+ <?PHP include locate_template('/feed/rss-one.php'); ?>
306
+
307
+ <?php endwhile; ?>
308
+
309
+ <?php // 日付でソート
310
+
311
+ foreach ((array)$bloglist as $key => $value) {
312
+
313
+ $sort[$key] = $value['update'];
314
+
315
+ }
316
+
317
+ array_multisort($sort, SORT_DESC, $bloglist);
318
+
319
+ ?>
320
+
321
+ <?php
322
+
323
+ for($i = 0 ; $i < count($bloglist); $i++){
324
+
325
+ echo $bloglist[$i]['text'] ;
326
+
327
+ }
328
+
329
+ ?>
330
+
331
+ <?php endif; ?>
332
+
333
+ <?php wp_reset_postdata(); ?>
334
+
335
+ </ul>
336
+
337
+ ```
338
+
339
+ ↓rss-one.php
340
+
341
+ ```php
342
+
343
+ <?php
344
+
345
+ require_once "Feed.php" ; //rss-phpライブラリを読み込みます
346
+
347
+ $feed = new Feed ;
348
+
349
+ date_default_timezone_set('Asia/Tokyo');
350
+
351
+ $txt = get_field('rss');//←カスタムフィールドを取得
352
+
353
+ if($txt){
354
+
355
+ $url = $txt;
356
+
357
+ $rss = $feed->loadRss( $url ) ;
358
+
359
+ $num = 1;//表示させたい件数
360
+
361
+ $i=0;
362
+
363
+ $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
364
+
365
+ if ( $desW != 0){
366
+
367
+ $desW = ($desW*2)+2;
368
+
369
+ }
370
+
371
+ foreach( $rss->item as $item )
372
+
373
+ {
374
+
375
+ if($i>=$num){
376
+
377
+ }
378
+
379
+ else{
380
+
381
+ $title = $item->title ; // タイトル
382
+
383
+ $link = $item->link ; // リンク
384
+
385
+ $timestamp = strtotime( $item->pubDate ) ; // 更新日時
386
+
387
+ $description = $item->description ; // 詳細
388
+
389
+ $description = str_replace("▼続きを読む","",$description);
390
+
391
+ //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する
392
+
393
+ $description = strip_tags($description);
394
+
395
+ if ( $desW != 0){
396
+
397
+ $description = mb_strimwidth($description, 0, $desW, "…",'utf-8');
398
+
399
+ }
400
+
401
+ ?>
402
+
403
+ <?php
404
+
405
+ $html = "<li class='rss'>";
406
+
407
+ $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
408
+
409
+ $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
410
+
411
+ $html .="<p class='areaname'>";
412
+
413
+ $terms = get_the_terms($post->ID,'area');
414
+
415
+ foreach( $terms as $term ) {
416
+
417
+ $html .= $term->name;
418
+
419
+ };
420
+
421
+ $html .="</p>";
422
+
423
+ $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
424
+
425
+ $html .="</li>";
426
+
427
+ ?>
428
+
429
+ <?php $shopblog = array(
430
+
431
+ "text"=>$html,
432
+
433
+ "update"=>date( 'YmdHi', $timestamp)
434
+
435
+ );
436
+
437
+ ?>
438
+
439
+ <?php
440
+
441
+ $i++;
442
+
443
+ $bloglist[]=$shopblog;
444
+
445
+ }
446
+
447
+ }
448
+
449
+ }
450
+
451
+ ?>
452
+
453
+
454
+
455
+ ```
456
+
457
+
458
+
459
+ ありがとうございました!

5

追記修正

2017/06/01 17:24

投稿

thewild
thewild

スコア32

test CHANGED
File without changes
test CHANGED
@@ -200,144 +200,20 @@
200
200
 
201
201
 
202
202
 
203
+
204
+
203
- ##追記
205
+ ###不明点
204
-
206
+
205
- ```php
207
+ ```php
206
-
207
- <?php
208
+
208
-
209
- require_once "Feed.php" ; //rss-phpライブラリを読み込みます
210
-
211
- $feed = new Feed ;
212
-
213
- date_default_timezone_set('Asia/Tokyo');
214
-
215
- $txt = get_field('rss');//←カスタムフィールドを取得
216
-
217
- if($txt){
218
-
219
- $url = $txt;
220
-
221
- $rss = $feed->loadRss( $url ) ;
222
-
223
- $num = 1;//表示させたい件数
224
-
225
- $i=0;
226
-
227
- $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
228
-
229
- if ( $desW != 0){
230
-
231
- $desW = ($desW*2)+2;
232
-
233
- }
234
-
235
- foreach( $rss->item as $item )
236
-
237
- {
238
-
239
- if($i>=$num){
240
-
241
- }
242
-
243
- else{
244
-
245
- $title = $item->title ; // タイトル
246
-
247
- $link = $item->link ; // リンク
248
-
249
- $timestamp = strtotime( $item->pubDate ) ; // 更新日時
250
-
251
- $description = $item->description ; // 詳細
252
-
253
- $description = str_replace("▼続きを読む","",$description);
254
-
255
- //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する
256
-
257
- $description = strip_tags($description);
258
-
259
- if ( $desW != 0){
260
-
261
- $description = mb_strimwidth($description, 0, $desW, "…",'utf-8');
262
-
263
- }
264
-
265
- ?>
266
-
267
- /////////ここから変更////////////
268
-
269
- <?php
270
-
271
- $html = "<li class='rss'>";
272
-
273
- $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
274
-
275
- $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
276
-
277
- $html .="<p class='areaname'>";
278
-
279
- $terms = get_the_terms($post->ID,'area');
280
-
281
- foreach( $terms as $term ) {
282
-
283
- $html .= $term->name;
284
-
285
- };
286
-
287
- $html .="</p>";
288
-
289
- $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
290
-
291
- $html .="</li>";
292
-
293
- ?>
294
-
295
- <?php $shopblog = array(
209
+ <?php $shopblog = array(
296
-
210
+
297
- "text"=>$html,
211
+ "text"=>$html,
298
-
212
+
299
- "update"=>date( 'YmdHi', $timestamp)
213
+ "update"=>date( 'YmdHi', $timestamp)
300
214
 
301
215
  );
302
216
 
303
- $bloglist = array();
304
-
305
- ?>
306
-
307
- <?php
308
-
309
- $i++;
310
-
311
- $bloglist []=$shopblog;
312
-
313
- var_dump($bloglist);
314
-
315
- }
316
-
317
- }
318
-
319
- }
320
-
321
- ?>
322
-
323
-
324
-
325
- ```
326
-
327
-
328
-
329
- ###不明点
330
-
331
- ```php
332
-
333
- <?php $shopblog = array(
334
-
335
- "text"=>$html,
336
-
337
- "update"=>date( 'YmdHi', $timestamp)
338
-
339
- );
340
-
341
217
  ```
342
218
 
343
219
 
@@ -383,3 +259,7 @@
383
259
  これは例ですが、このarrayの中にカスタムフィールドで取得した値をループで格納する方法が分からなかったのと、
384
260
 
385
261
  カスタム投稿shopのタイトル(店舗名)と、タームを関連付ける方法が分からなかったです。
262
+
263
+
264
+
265
+ よろしくお願いいたします。

4

コード内容を現状に

2017/06/01 15:53

投稿

thewild
thewild

スコア32

test CHANGED
File without changes
test CHANGED
@@ -28,29 +28,37 @@
28
28
 
29
29
  <?php
30
30
 
31
- $args = array(
31
+ $args = array(
32
-
32
+
33
- 'posts_per_page' => 7,
33
+ 'posts_per_page' => 7,
34
-
34
+
35
- 'paged' => $paged,
35
+ 'paged' => $paged,
36
-
36
+
37
- 'orderby' => 'post_date',//ここで指定してもカスタム投稿の投稿順になるだけなので意味がない。
37
+ 'orderby' => 'post_date',
38
-
38
+
39
- 'post_type' => 'shop',//shopという名のカスタム投稿
39
+ 'post_type' => 'shop',
40
-
40
+
41
- 'post_status' => 'publish'
41
+ 'post_status' => 'publish'
42
-
42
+
43
- );
43
+ );
44
-
44
+
45
- $the_query = new WP_Query($args);?>
45
+ $the_query = new WP_Query($args);
46
-
46
+
47
- <?PHP
47
+ ?>
48
-
48
+
49
- if ( $the_query->have_posts() ) :?>
49
+ <?php if ( $the_query->have_posts() ) :?>
50
50
 
51
51
  <?php while ( $the_query->have_posts() ) : $the_query->the_post();?>
52
52
 
53
+ <?php
54
+
55
+ $bloglist = array();
56
+
53
- <?PHP get_template_part('feed/rss-one'); ?>
57
+ get_template_part('feed/rss-one');
58
+
59
+ var_dump($bloglist);
60
+
61
+ ?>
54
62
 
55
63
  <?php endwhile; ?>
56
64
 
@@ -76,7 +84,7 @@
76
84
 
77
85
  date_default_timezone_set('Asia/Tokyo');
78
86
 
79
- $txt = get_field('rss');//←カスタムフィールドでurlを取得
87
+ $txt = get_field('rss');//←カスタムフィールドを取得
80
88
 
81
89
  if($txt){
82
90
 
@@ -88,7 +96,7 @@
88
96
 
89
97
  $i=0;
90
98
 
91
- $desW = 50;
99
+ $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
92
100
 
93
101
  if ( $desW != 0){
94
102
 
@@ -128,36 +136,48 @@
128
136
 
129
137
  ?>
130
138
 
131
- <li class="rss">
132
-
133
- <p class="post-date"><?php echo date( "H:i", $timestamp); ?></p>
134
-
135
- <h3><a href="<?php echo $link; ?>" target="_blank"><?php echo $title; ?></a></h3>
136
-
137
- <p class="areaname">
138
-
139
- <?php
139
+ <?php
140
+
140
-
141
+ $html = "<li class='rss'>";
142
+
143
+ $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
144
+
145
+ $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
146
+
147
+ $html .="<p class='areaname'>";
148
+
141
- $terms = get_the_terms($post->ID,'area');
149
+ $terms = get_the_terms($post->ID,'area');
142
-
150
+
143
- foreach( $terms as $term ) {
151
+ foreach( $terms as $term ) {
144
-
152
+
145
- echo $term->name;
153
+ $html .= $term->name;
146
-
154
+
147
- }
155
+ };
156
+
148
-
157
+ $html .="</p>";
158
+
159
+ $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
160
+
161
+ $html .="</li>";
162
+
149
- ?>
163
+ ?>
164
+
150
-
165
+ <?php $shopblog = array(
166
+
151
- </p>
167
+ "text"=>$html,
152
-
168
+
153
- <p class="shopname"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
169
+ "update"=>date( 'YmdHi', $timestamp)
170
+
154
-
171
+ );
172
+
155
- </li>
173
+ ?>
156
174
 
157
175
  <?php
158
176
 
159
177
  $i++;
160
178
 
179
+ $bloglist[]=$shopblog;
180
+
161
181
  }
162
182
 
163
183
  }

3

コード内容を現状に修正

2017/06/01 15:48

投稿

thewild
thewild

スコア32

test CHANGED
File without changes
test CHANGED
File without changes

2

試した方法を追加

2017/06/01 15:41

投稿

thewild
thewild

スコア32

test CHANGED
File without changes
test CHANGED
@@ -328,4 +328,38 @@
328
328
 
329
329
  ・ループで出力したこの値を新たに変数に格納する方法
330
330
 
331
+ ・出力する方法
332
+
331
333
  が不明です。
334
+
335
+
336
+
337
+ ##試した方法
338
+
339
+ ちなみにkei344さんが添付してくださったリンク先の方法もはじめに試しましたが、
340
+
341
+ ```php
342
+
343
+ // 取得するフィードのURLを指定
344
+
345
+ $urls = array(
346
+
347
+ "http://www.lesson5.info/?feed=rss2",
348
+
349
+ "http://www.lesson5.info/?feed=rss2",
350
+
351
+ "http://www.lesson5.info/?feed=rss2",
352
+
353
+ "http://www.lesson5.info/?feed=rss2",
354
+
355
+ "http://www.lesson5.info/?feed=rss2",
356
+
357
+ "http://www.lesson5.info/?feed=rss2"
358
+
359
+ );
360
+
361
+ ```
362
+
363
+ これは例ですが、このarrayの中にカスタムフィールドで取得した値をループで格納する方法が分からなかったのと、
364
+
365
+ カスタム投稿shopのタイトル(店舗名)と、タームを関連付ける方法が分からなかったです。

1

追記

2017/05/31 19:27

投稿

thewild
thewild

スコア32

test CHANGED
File without changes
test CHANGED
@@ -177,3 +177,155 @@
177
177
  店舗ごとに外部ブログがあり、最新記事を一つだけ表示するようにしてます。
178
178
 
179
179
  当たり前ですが現在はrssの更新順ではなく、ワードプレスに投稿した記事の日付順になってますので、これをこの形のままrssの更新順にしたいです。
180
+
181
+
182
+
183
+ ##追記
184
+
185
+ ```php
186
+
187
+ <?php
188
+
189
+ require_once "Feed.php" ; //rss-phpライブラリを読み込みます
190
+
191
+ $feed = new Feed ;
192
+
193
+ date_default_timezone_set('Asia/Tokyo');
194
+
195
+ $txt = get_field('rss');//←カスタムフィールドを取得
196
+
197
+ if($txt){
198
+
199
+ $url = $txt;
200
+
201
+ $rss = $feed->loadRss( $url ) ;
202
+
203
+ $num = 1;//表示させたい件数
204
+
205
+ $i=0;
206
+
207
+ $desW = 50;//詳細の文字数を制限します。制限しないときは0にします。
208
+
209
+ if ( $desW != 0){
210
+
211
+ $desW = ($desW*2)+2;
212
+
213
+ }
214
+
215
+ foreach( $rss->item as $item )
216
+
217
+ {
218
+
219
+ if($i>=$num){
220
+
221
+ }
222
+
223
+ else{
224
+
225
+ $title = $item->title ; // タイトル
226
+
227
+ $link = $item->link ; // リンク
228
+
229
+ $timestamp = strtotime( $item->pubDate ) ; // 更新日時
230
+
231
+ $description = $item->description ; // 詳細
232
+
233
+ $description = str_replace("▼続きを読む","",$description);
234
+
235
+ //↑ 続きを読むなど、決まった文章が詳細にはいっている場合に、それを除外する
236
+
237
+ $description = strip_tags($description);
238
+
239
+ if ( $desW != 0){
240
+
241
+ $description = mb_strimwidth($description, 0, $desW, "…",'utf-8');
242
+
243
+ }
244
+
245
+ ?>
246
+
247
+ /////////ここから変更////////////
248
+
249
+ <?php
250
+
251
+ $html = "<li class='rss'>";
252
+
253
+ $html .="<p class='post-date'>" . date( 'H:i', $timestamp) . "</p>";
254
+
255
+ $html .="<h3><a href=" . $link . "target='_blank'>" . $title . "</a></h3>";
256
+
257
+ $html .="<p class='areaname'>";
258
+
259
+ $terms = get_the_terms($post->ID,'area');
260
+
261
+ foreach( $terms as $term ) {
262
+
263
+ $html .= $term->name;
264
+
265
+ };
266
+
267
+ $html .="</p>";
268
+
269
+ $html .="<p class='shopname'><a href=" . get_permalink($post->ID) .">" . the_title('','',false) . "</a></p>";
270
+
271
+ $html .="</li>";
272
+
273
+ ?>
274
+
275
+ <?php $shopblog = array(
276
+
277
+ "text"=>$html,
278
+
279
+ "update"=>date( 'YmdHi', $timestamp)
280
+
281
+ );
282
+
283
+ $bloglist = array();
284
+
285
+ ?>
286
+
287
+ <?php
288
+
289
+ $i++;
290
+
291
+ $bloglist []=$shopblog;
292
+
293
+ var_dump($bloglist);
294
+
295
+ }
296
+
297
+ }
298
+
299
+ }
300
+
301
+ ?>
302
+
303
+
304
+
305
+ ```
306
+
307
+
308
+
309
+ ###不明点
310
+
311
+ ```php
312
+
313
+ <?php $shopblog = array(
314
+
315
+ "text"=>$html,
316
+
317
+ "update"=>date( 'YmdHi', $timestamp)
318
+
319
+ );
320
+
321
+ ```
322
+
323
+
324
+
325
+ という部分で$shopblogにテキストデータと、日付を関連付けて格納したのですが、
326
+
327
+ ・新たに変数に格納するのを記述する場所
328
+
329
+ ・ループで出力したこの値を新たに変数に格納する方法
330
+
331
+ が不明です。